#28072: Allow QuerySet.annotate() to use the name of an exisiting field
-------------------------------------+-------------------------------------
Reporter: Marcin Nowak | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Evan Fiddler):
Is there still interest from anyone else on this? I accomplished this is
an abstracted version of QuerySet that my company uses internally.
Approved to make a PR against Django Project if there’s interest.
The one main concern I have on this idea is in two parts:
1. In order to successfully annotate the queryset with a name that’s
already used (and for it to be at all useful) the original value must be
removed from the queryset.
2. This is very doable by pulling current values, removing the “old” key,
then applying the annotate to the queryset. All very doable.
However, this returns an **incredibly** unsafe queryset which if we see in
anything but a read-only way could overwrite data in the model/db.
So, here’s my solution: make ANOTHER method (`annotate_override()` or
`annotate_read_only()`or something like that) that returns the annotated
queryset with the old name/value removed and the passed-in expression
annotated.
One issue I see with this is that it’s returning a true queryset and
therefore, in order for THIS to work well, we may need another abstracted
class on `QuerySet` like `ReadOnlyQuerySet(QuerySet)` or something like
that…which makes this a much larger change.
Alternatively, the doc string on this new method could be incredibly
verbose with warnings about usage.
Lastly, we could just provide a k, v param to `annotate()` (e.g.
`annotate(<annotation expression>, override_model=True)` and the change is
smaller but less safe.
If this sound good, I’ll assign and make the PR. I’d like some feedback on
this first though.
--
Ticket URL: <https://code.djangoproject.com/ticket/28072#comment:5>
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 view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018273d6facf-08fe5fe7-1161-4eb7-9538-bfe10df86e90-000000%40eu-central-1.amazonses.com.