39👍
You can do this using an F expression:
from django.db.models import F
tasks = Task.objects.filter(task_definition__cascades=False)
.update(shared_task_id=F('id'))
There are some restrictions on what you can do with F
objects in an update
call, but it’ll work fine for this case:
Calls to update can also use
F expressions
to update one field based on the value of another field in the model.However, unlike
F()
objects infilter
andexclude
clauses, you can’t introduce joins when you useF()
objects in anupdate
– you can only reference fields local to the model being updated. If you attempt to introduce a join with anF()
object, aFieldError
will be raised[.]
https://docs.djangoproject.com/en/dev/topics/db/queries/#updating-multiple-objects-at-once
5👍
I stumbled upon this topic and noticed Django’s limitation of updates with foreign keys, so I now use raw SQL in Django:
from django.db import connection
with connection.cursor() as cursor:
cursor.execute("UPDATE a JOIN b ON a.b_id = b.id SET a.myField = b.myField")
- Improving Performance of Django ForeignKey Fields in Admin
- Change default Django REST Framework home page title
- Django __call__() missing 1 required keyword-only argument: 'manager'
- Registered models do not show up in admin
- Problem launching docker-compose : python modules not installed