Opened 9 years ago
Closed 8 years ago
#26517 closed Bug (fixed)
Empty Querset Using Empty Queryset in ExpressionWrapper
Reported by: | Sven R. Kunze | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
We use the ExpressionWrapper + annotate to annotate specific boolean values to all queried instances like this (minimal working example):
editable_ones = ..... # another queryset result = MyModel.objects.annotate(editable=ExpressionWrapper(Q(pk__in=editable_ones), output_field=BooleanField())
As soon as editable_ones
is empty, result
is also empty. Reading the docs, I guess this is not desired behavior (https://docs.djangoproject.com/en/1.9/ref/models/querysets/#annotate).
Change History (9)
comment:1 by , 9 years ago
Description: | modified (diff) |
---|
comment:2 by , 9 years ago
comment:3 by , 9 years ago
Triage Stage: | Unreviewed → Accepted |
---|---|
Version: | 1.8 → master |
comment:5 by , 9 years ago
FYI ExpressionWrapper was never meant to be used to wrap up Q() objects. Q() are not really expressions themselves, they just implement some of the same API internally.
You're much better off using Case Expressions https://docs.djangoproject.com/en/1.9/ref/models/conditional-expressions/ for this use case:
MyModel.objects.annotate(editable=Case(When(pk__in=[editable], then=1), default=0, output_field=BooleanField()) )
comment:6 by , 8 years ago
Patch needs improvement: | set |
---|
comment:7 by , 8 years ago
Patch needs improvement: | unset |
---|
comment:8 by , 8 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
I did some further analysis:
Both results differ.
MyModel.objects.none()
forces the outer query to return an empty QuerySet.