#22349 closed Cleanup/optimization (fixed)
RawQuerySet doesn't implement __len__, __bool__
Reported by: | Craig de Stigter | Owned by: | |
---|---|---|---|
Component: | Documentation | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | loic@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
RawQuerySet should mirror QuerySet behaviour where possible.
However, RawQuerySet.__bool__
always returns True, instead of evaluating the query results and checking the length is nonzero.
RawQuerySet.__len__
doesn't work at all:
* TypeError: object of type 'RawQuerySet' has no len()
This is not intuitive since it's not how QuerySet works. It basically means that you have to cast RawQuerySet to a list before doing much with its results.
Change History (14)
comment:1 by , 11 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 11 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 11 years ago
Owner: | removed |
---|---|
Status: | assigned → new |
comment:4 by , 11 years ago
Dunno if it's by design, but RawQuerySet
and QuerySet
operate vastly differently as there is no such thing as an "evaluated" RawQuerySet
:
list(queryset) with assertNumQueries(1) for i in range(10): queryset[0] list(raw_queryset) with assertNumQueries(10) for i in range(10): raw_queryset[0]
This fundamental difference makes having a shared ancestor impractical.
With that in mind, I think adding __len__
would be a nasty footgun, if you only want the number of rows, you can write a COUNT
SQL query, if you want the count and the result, then you should definitely store the result in a list to avoid a second query.
Of course we could make RawQuerySet
maintain an internal cache akin to QuerySet
, but that'd have to be a ticket in its own right.
comment:5 by , 11 years ago
Wow, really? That's crazy. I had no idea. Perhaps the docs should make mention of that? I have been using RawQueryset for UPDATE ... RETURNING *
queries and I'll need to check my code for bugs if that's the case.
Either we should add the internal cache, or at least the docs should add some big huge scary warning about that inconsistency. https://docs.djangoproject.com/en/dev/topics/db/sql/#django.db.models.Manager.raw
comment:6 by , 11 years ago
Cc: | added |
---|
comment:7 by , 11 years ago
We discussed this with Loic on IRC. Introducing __len__
and friends without caching the results is a bad idea. One possibility considered was adding caching to RawQuerySet so that it works like QuerySet, and also add .iterator() method to RawQuerySet so that when iterating over large queryset one can avoid the performance and memory penalty of caching.
There doesn't seem to be a way to introduce this change using deprecation path or check framework, so we would need to just do the change and warn about the change in release notes. As the current behavior doesn't seem to be a big pain point for users, and some users could be relying on current behavior, I think it is better to leave things as they are. However, we should add some documentation about the evaluation time differences between qs and rawqs, so I'll leave this ticket open for that.
comment:8 by , 11 years ago
Component: | Database layer (models, ORM) → Documentation |
---|---|
Needs documentation: | set |
Type: | Bug → Cleanup/optimization |
Moving back to documentation based on the provided feedback.
comment:10 by , 10 years ago
Needs documentation: | unset |
---|
comment:11 by , 10 years ago
Owner: | set to |
---|---|
Resolution: | → fixed |
Status: | new → closed |
For the sake of consistency and DRYness it might be worth creating common ancestor class for
QuerySet
andRawQuerySet
(sayBaseQuerySet
) that implements common operations.