22👍
You make a false assumption. Django does not retrieve all objects when paginating: it slices the queryset appropriately, which uses LIMIT and COUNT on the SQL.
1👍
A QuerySet
is a lazy object. When you assign contact_list = Contacts.objects.all()
, Django will NOT hit the database. Until you call the methods such as count()
, filter()
, first()
,…, or contact_list[1:5]
, Django will really retrieve data from the database. SQL statements that Django generate will correlate to each method and these SQL statments will be sent to the DB.
E.g: contact_list[1:5]
generate a SQL statement have LIMIT
and OFFSET
clauses.
In Paginator
class, the QuerySet
will passed to the its constructor
paginator = Paginator(contact_list, 25)
When you call paginator.page(page)
, the statement is called:
return self._get_page(self.object_list[bottom:top], number, self)
- How to generate presigned S3 urls using django-storages?
- Django limit_choices_to on user group
- Django REST Framework – multiple models / APIs?
0👍
Look inside Paginator class (django/core/paginator.py), it fetches only required pages. There is only one problem on big tables: if you want to show total page numbers you must make count(*) on entire table which can took a long time in some databases (i.e. postgresql, mysql with innodb).
BTW, try to use generic views in django, ListView would be fine here.
- (Re)Checking Dependencies with PIP
- What is a more efficient way to pass variables from Template to View in Django?
0👍
Here we using get_page()
to get page wise data(1 page contain 25 data).I would suggest for this like :
def listing(request):
contact_list = Contacts.objects.all()
paginator = Paginator(contact_list, 25) # Show 25 contacts per page
page = request.GET.get('page')
contacts = paginator.get_page(page)
return render_to_response('list.html', {"contacts": contacts})
- Invalid block tag: expected 'elif', 'else' or 'endif'
- How to profile Django on Gunicorn in production