Answer the question
In order to leave comments, you need to log in
How to display the 5 most recent posts sorted by views?
Hi friends. There is a model:
class Article(models.Model):
...
is_published = models.BooleanField("Опубликовать?", default=True,)
pub_date = models.DateTimeField("Дата обновления", auto_now_add=True, blank=True, null=True)
views = models.PositiveIntegerField("Просмотры", default=0)
# Первый вариант:
article_list = Article.objects.filter(is_published=True).order_by("-pub_date", "-views",)[:5]
# Второй вариант:
article_list = Article.objects.filter(is_published=True).order_by("-views", "-pub_date",)[:5]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question