diff --git a/apps/news/models.py b/apps/news/models.py index 1b9023db..cfb10d39 100644 --- a/apps/news/models.py +++ b/apps/news/models.py @@ -5,7 +5,7 @@ from django.utils import timezone from django.utils.translation import gettext_lazy as _ from rest_framework.reverse import reverse 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): @@ -104,23 +104,18 @@ class News(BaseAttributes, TranslatedFieldsMixin): @property 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() - news_content_type = ContentType.objects.get(app_label="news", model="news") - # - like_news = News.objects.filter(is_publish=True, news_type=self.news_type, - tags__content_type=news_content_type) + if news_count >= 6: + random_ids = random_sample(range(news_count), 6) + else: + random_ids = random_sample(range(news_count), news_count) - # - # 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] + news_list = [{"id": like_news[r].id, "slug": like_news[r].slug} for r in random_ids] return news_list