invalid count result when paired with select_related
|Reported by:||Mathieu Bouchard <mbouchar@…>||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.0|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
When using a select_related, I sometimes get invalid results if the select_related method reject some data because they are not valid.
If, for example, I have a model that requires a reference to another object and that reference is, for some reason, None, these entries
will be rejected by the select_related.
The problem is that the Paginator object uses the count method to tell the user how many items are found, but when we try to use these items, we only get an empty page.
>>> a = Session.objects.filter(Key__value = "www").select_related() >>> a.count() 29L >>> len(a) 3
>>> from django.core.paginator import Paginator >>> from metrix2.manager.models import Session >>> q = Session.objects.filter(Key__string = "2PSpuaRIZGNj").select_related() >>> p = Paginator(q, 15) >>> p.count 29L >>> p.num_pages 2 >>> p.page(1).object_list [<Session: xxx>, <Session: yyy>, <Session: zzz>] >>> p.page(2).object_list 
Change History (7)
comment:1 Changed 6 years ago by julien
- Component changed from Uncategorized to Database layer (models, ORM)
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
comment:2 Changed 6 years ago by jacob
- milestone set to 1.1
- Triage Stage changed from Unreviewed to Accepted
comment:6 Changed 6 years ago by mtredinnick
- Resolution set to invalid
- Status changed from new to closed