Issue
If I will use
videos = Videos.objects.all()
and then
paginator = Paginator(videos, 9)
and I have 1 000 000 records, then Videos.objects.all() will make a query
SELECT * FROM...?
Is it a proper way? Or I have to use LIMIT parameter query to paginate properly?
Solution
Just like what the other comments says when you execute Videos.objects.all(), Django creates a queryset that represents all videos in the database. However, Django querysets are "lazy" - they don't hit the database until they are evaluated. This means that if you have a QuerySet representing a million videos, Django won't actually load all those videos into memory until you do something with the QuerySet that requires it to be evaluated, like iterating over it.
When you pass a QuerySet to Django's Paginator and ask for a specific page, Django is smart enough to only query the database for the objects that belong to that page. So even if your QuerySet represents a million videos, if your page size is 9, Django will only load 9 videos into memory.
So, in your case, using Videos.objects.all() with Django's Paginator is the correct and efficient way to paginate your videos, even if you have a million of them.
Answered By - Winmari Manzano
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.