This commit is contained in:
michail 2019-09-27 17:12:24 +05:00
parent 962dc2f480
commit 755490c6cf

View File

@ -5,7 +5,7 @@ from django.utils import timezone
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from rest_framework.reverse import reverse from rest_framework.reverse import reverse
from utils.models import BaseAttributes, TJSONField, TranslatedFieldsMixin from utils.models import BaseAttributes, TJSONField, TranslatedFieldsMixin
from django.contrib.contenttypes.models import ContentType from random import sample as random_sample
class NewsType(models.Model): class NewsType(models.Model):
@ -104,23 +104,18 @@ class News(BaseAttributes, TranslatedFieldsMixin):
@property @property
def list_also_like_news(self): def list_also_like_news(self):
# news_content_type = ContentType.objects.get(app_label="news", model="news")
# tg_name = self.tags.filter(metadata__content_type=news_content_type) # without "distinct" method the doubles are arising
like_news = News.objects.published().filter(news_type=self.news_type, tags__in=models.F("tags"))\
.exclude(id=self.id).distinct()
# print(tg_name) news_count = like_news.count()
# tag_id = self.tags.all() if news_count >= 6:
news_content_type = ContentType.objects.get(app_label="news", model="news") random_ids = random_sample(range(news_count), 6)
# else:
like_news = News.objects.filter(is_publish=True, news_type=self.news_type, random_ids = random_sample(range(news_count), news_count)
tags__content_type=news_content_type)
# news_list = [{"id": like_news[r].id, "slug": like_news[r].slug} for r in random_ids]
# print("LINE 112", like_news)
# news_list = [self.description, "extra", "field", "test", self.slug]
news_list = [{"id": n.id, "slug": n.slug} for n in like_news]
return news_list return news_list