Issue
I have a ListView
with pagination:
class StoriesListView(ListView):
model = Story
paginate_by = 20
def get_queryset(self):
return Story.objects.all().order_by('-updated_at')
I have 1000 Story
objects in the database. What happens when the user loads my view? Will all the 1000 be queried from the database or only 20? How can I optimize this?
Solution
It depends on how you are using it. QuerySet objects are lazy, and in this particular case, the SQL query will add LIMIT
and OFFSET
, so the query will always return only 20 results. But when you want to use related objects in your template, you have to optimize your query with select_related
or prefetch_related
methods ( otherwise you will end up with additional queries for each record from the main query )
I think you should read how to optimize database access in django framework.
Hope this helps.
Answered By - dydek
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.