K
K
KirillTrueno2022-01-18 01:17:13
Django
KirillTrueno, 2022-01-18 01:17:13

Django queryset: how to get last 3 rows of result?

Article.objects.filter(date__date=today).order_by('date')[:3]
returns the first 3 rows sorted by date ascending .

Article.objects.filter(date__date=today).order_by('-date')[:3]
returns the last 3 rows sorted by date descending .

Article.objects.filter(date__date=today).order_by('-date')[:3]
Article.reverse()
returns an error.

How to select last 3 rows but sorted by date ascending ?
Article.objects.filter(date__date=today).order_by('date')[последние 3 строки]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
KirillTrueno, 2022-01-18
@KirillTrueno

Decision:

articles = list(Article.objects.filter(date__date=today).order_by('-date')[:3])
articles.reverse()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question