8👍
User.objects.filter(is_active=True).order_by("-date_joined")[:10]
will give you the last 10 users who joined. See the Django docs for details.
4👍
Use list slice operation on constructed queries e.g.
For example, this returns the first 5 objects (LIMIT 5):
Entry.objects.all()[:5]
This returns the sixth through tenth objects (OFFSET 5 LIMIT 5):
Entry.objects.all()[5:10]
Read django documentation
http://docs.djangoproject.com/en/dev/topics/db/queries/
- [Django]-Django ORM: Joining QuerySets
- [Django]-Google OAUTH gives 502 error
- [Django]-Django – 'module' object is not callable
- [Django]-Django: ImportError: cannot import name 'GeoIP2'
- [Django]-Best practice – Django multisite
2👍
User.objects.filter(is_active=True).order_by("-date_joined")[:10]
QuerySets are lazy [ http://docs.djangoproject.com/en/dev/ref/models/querysets/ ]. When you’re slicing the list, it doesn’t actually fetch all entries, load them up into a list, and then slice them. Instead, it fetches only the last 10 entries.
- [Django]-How to save django FileField to user folder?
- [Django]-How to unit test image upload in Django REST Framework
- [Django]-Get a dropdown of states in Django Admin
- [Django]-Django with django-nose: two identical settings files with different behavior in running test command
0👍
BTW, if u would just divide amount of all user, (for example, 10 per site) you should interest pagination in dJango, very helpful http://docs.djangoproject.com/en/dev/topics/pagination/#topics-pagination
- [Django]-Django password hash different everytime
- [Django]-Django – putting HTML tags inside blocks of text subject to translation
- [Django]-How to use has_object_permission with APIView in Django Rest Framework?
- [Django]-How to make a geography field unique?
- [Django]-Django Model API reverse lookup of many to many relationship through intermediary table