M
M
Maxim Vlasov2021-11-03 21:21:02
Django
Maxim Vlasov, 2021-11-03 21:21:02

Why does django have such weird pagination?

Hello! I recently started learning django and came across a very strange thing when using pagination (this is after a dozen years of working with PHP).
The documentation has an example:

contact_list = Contact.objects.all() # Получить ВСЕ(!!!) записи???
paginator = Paginator(contact_list, 25) # Разделить список на чанки/страницы


From the point of view of the algorithm, this is complete nonsense. Maybe I do not know the mechanism of some kind in this design. But selecting all records from the database in order to share them is crazy. And if there are 10 million of these records? Or does using such a construct create a query with `COUNT` under the hood? After PHP, it seemed like the perfect framework. And here is such a disappointment. How does it work anyway?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-11-03
@tr0yka

The Queryset is lazy, no fetching occurs unless the contact_list is iterated in some way. The paginator under the hood will simply add the parameters for selecting the desired page to the uncomputed queryset and pass it to the context of the template engine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question