{{{
class Child(models.Model):
pass
class Parent(models.Model):
child = models.ForeignKey(Child, on_delete=models.CASCADE, null=True)
}}}
{{{
parent = Parent.objects.create(child=None)
parent.child = Child()
parent.child.save()
Parent.objects.bulk_update([parent], fields=["child"])
}}}
Expected behavior:
parent model instance was updated with the ID to the child in the
database.
Actual behavior:
Parent model is still referencing Null.
There should probably be some check for ForeignKeys in the bulk_update
logic, and if one is updated, the ID of the child model should be re-
copied to the child_id field that is actually being written to the
database.
--
Ticket URL: <https://code.djangoproject.com/ticket/33322>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* cc: Hannes Ljungberg (added)
* stage: Unreviewed => Accepted
Comment:
Thanks for the report. This is because `child_id` is not updated after
`parent.child.save()`.
Related to #29497 and #32133.
--
Ticket URL: <https://code.djangoproject.com/ticket/33322#comment:1>
* owner: nobody => Hannes Ljungberg
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/33322#comment:2>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/33322#comment:3>
* cc: Egor R (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/33322#comment:4>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/33322#comment:5>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"ed2018037d152eef7e68f339b4562f8aadc2b7a0" ed201803]:
{{{
#!CommitTicketReference repository=""
revision="ed2018037d152eef7e68f339b4562f8aadc2b7a0"
Fixed #33322 -- Fixed loss of assigned related object when saving relation
with bulk_update().
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33322#comment:6>
Comment (by Manuel Baclet):
The PR was merged 6 months ago but it has not landed in any release. Can
someone add this to the current bugfix branch, please?
--
Ticket URL: <https://code.djangoproject.com/ticket/33322#comment:7>
Comment (by Mariusz Felisiak):
Replying to [comment:7 Manuel Baclet]:
> The PR was merged 6 months ago but it has not landed in any release. Can
someone add this to the current bugfix branch, please?
This will be released in Django 4.1+.
--
Ticket URL: <https://code.djangoproject.com/ticket/33322#comment:8>