Limiting the results returned from a request in django, python

I just started learning how to make requests in a Django application, and I have a request that gets a list of new users filtered by join date:

newUsers = User.objects.filter(is_active=True).order_by("-date_joined")

This, as I understand it, gives me ALL users sorted by date. What is the best way to limit it to get the last N users from me?

On this, does anyone recommend and read material to learn more about these types of queries?

+3
source share
4 answers
User.objects.filter(is_active=True).order_by("-date_joined")[:10]

will give you the last 10 users that have joined. See Django docs for more details .

+8
source

, .

, 5 (LIMIT 5):

Entry.objects.all()[:5]

- (OFFSET 5 LIMIT 5):

Entry.objects.all()[5:10]

django http://docs.djangoproject.com/en/dev/topics/db/queries/

+4
User.objects.filter(is_active=True).order_by("-date_joined")[:10]

QuerySets [http://docs.djangoproject.com/en/dev/ref/models/querysets/]. , , , . 10 .

+2

Source: https://habr.com/ru/post/1718369/


All Articles