#30061: annotate boolean from a Q object
-----------------------------------------+------------------------
Reporter: Ryan Hiebert | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 2.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
I think that I saw something about this on the Django-Developers mailing
list years ago, but I cannot find the reference, unfortunately.
I have on several occasions needed to annotate a boolean. The way that I
have found to do this is to use a conditional expression, but it seems
awfully noisy to my eyes. It seems like it might be better to allow
directly annotating Q objects, or perhaps providing a simple wrapper to
convert a Q object into a boolean field expression.
For example, instead of this:
{{{#!python
notes = notes.filter(user=self.user).annotate(
is_author=Case(
When(author_id=self.request.user.id, then=True),
default=Value(False),
output_field=BooleanField(),
),
)
}}}
I think that there's a good case to be made that this should be possible:
{{{#!python
notes = notes.filter(user=self.user).annotate(
is_author=Q(author_id=self.request.user.id),
)
}}}
Or if we really can't have the `Q` object doing double duty, we could wrap
that Q object in some expression constructor. I didn't provide an example,
because I couldn't think of a great and obvious name for such a
constructor.
--
Ticket URL: <https://code.djangoproject.com/ticket/30061>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/054.9d02d84043757b8e3c412cb433122981%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.