[Django] #32021: Add support to update a JSONField with result from annotation

10 views
Skip to first unread message

Django

unread,
Sep 20, 2020, 12:08:13 PM9/20/20
to django-...@googlegroups.com
#32021: Add support to update a JSONField with result from annotation
-------------------------------------+-------------------------------------
Reporter: Xavier | Owner: nobody
Lesa |
Type: New | Status: new
feature |
Component: Database | Version: 3.1
layer (models, ORM) | Keywords: JSONField, Models,
Severity: Normal | Annotate
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I have a model with a `JSONField` and want to update ''in just one query''
like any other Field. Something like this:

{{{
# one query update
related_machinery =
ActivityTracking.objects.filter(machinery=OuterRef('pk')).values('machinery')
sub_total =
related_machinery.annotate(Sum('measure_value')).values('measure_value__sum')
avg_total =
related_machinery.annotate(Avg('measure_value')).values('measure_value__avg')

Machinery.objects.filter(id=machinery_id).update(
measure_value=F('initial_measure_value') + sub_total,
metrics=avg_total
)
}}}


In example `measure_value` is a `DecimalField` and `metrics` is a
`JSONField`. Also will be great if keys in `JSONField` was accessibles in
`update`, like this:

{{{
Machinery.objects.filter(id=instance.machinery_id).update(
measure_value=F('initial_measure_value') + sub_total,
metrics__average_measure_value=avg_total
)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32021>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Reply all
Reply to author
Forward
0 new messages