[Django] #36218: allow annotate to override model's fields

15 views
Skip to first unread message

Django

unread,
Feb 28, 2025, 6:43:00 AM2/28/25
to django-...@googlegroups.com
#36218: allow annotate to override model's fields
-------------------------------------+-------------------------------------
Reporter: Jan Napieralski | Type: New
| feature
Status: new | Component: Database
| layer (models, ORM)
Version: 5.1 | Severity: Normal
Keywords: QuerySet.extra | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
I want to modify the QuerySet field such that it is fetched from DB and
its real field is redacted. For example fetching some employee data, but
replacing all the emails and phone numbers to NULL's because of some
security or role flag in the API resolver for this

Currently the .annotate() method doesn't allow for the overlap of output
field with model field. I have seen, that the way to handle it is to call
.values() excluding the overwritten field, but in the large models with
many fields or queries with joins, its not realistically possible to know
what fields will be used when doing this QuerySet manipulation. The
.deffer() doesn't work neither

I propose some flag to the .annotate() for allowing to overlap and
essentially overwrite the model field, bc currently the only way without
evaluating it, is the .extra()
--
Ticket URL: <https://code.djangoproject.com/ticket/36218>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 28, 2025, 7:42:22 AM2/28/25
to django-...@googlegroups.com
#36218: allow annotate to override model's fields
-------------------------------------+-------------------------------------
Reporter: Jan Napieralski | Owner: (none)
Type: New feature | Status: closed
Component: Database layer | Version: 5.1
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: QuerySet.extra | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by David Sanders):

* resolution: => wontfix
* status: new => closed

Comment:

Hi there, thanks for the idea though I believe (iirc) the original reason
this was prevented was to avoid accidental data loss when saving the
model.

I can't find the original thread discussing this but I did write up about
this here: https://github.com/shangxiao/stupid-django-
tricks/tree/master/annotation_overwrite

If this hack is unsuitable for you then I'd encourage you to start a
thread on the Django Forum, https://forum.djangoproject.com/, to discuss
the issue (any feature requests & proposals must get approval via the
forum first). It's unlikely to be accepted but there may be other folks
with other workarounds that may be your cup of tea 😉
--
Ticket URL: <https://code.djangoproject.com/ticket/36218#comment:1>

Django

unread,
Feb 28, 2025, 9:04:05 AM2/28/25
to django-...@googlegroups.com
#36218: allow annotate to override model's fields
-------------------------------------+-------------------------------------
Reporter: Jan Napieralski | Owner: (none)
Type: New feature | Status: closed
Component: Database layer | Version: 5.1
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: QuerySet.extra | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Jan Napieralski):

Thank you very much for the response, it was perfect for my case and its
more ORM like than .extra()
--
Ticket URL: <https://code.djangoproject.com/ticket/36218#comment:2>
Reply all
Reply to author
Forward
0 new messages