#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.
--
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/053.ef5ebea77620e037f1411ec4d4cfc24f%40djangoproject.com.