Opened 5 hours ago

Closed 4 hours ago

Last modified 3 hours ago

#36218 closed New feature (wontfix)

allow annotate to override model's fields

Reported by: Jan Napieralski Owned by:
Component: Database layer (models, ORM) Version: 5.1
Severity: Normal Keywords: QuerySet.extra
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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()

Change History (2)

comment:1 by David Sanders, 4 hours ago

Resolution: wontfix
Status: newclosed

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 😉

comment:2 by Jan Napieralski, 3 hours ago

Thank you very much for the response, it was perfect for my case and its more ORM like than .extra()

Note: See TracTickets for help on using tickets.
Back to Top