#25550 closed Cleanup/optimization (fixed)
Deprecate direct assignment to the reverse side of a related set
Reported by: | Tim Graham | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
As proposed by Ansii on django-developers:
>>> obj.reverse_fk_set = RelatedObj.objects.all()
What happens is that the RelatedObj.objects.all()
queryset is
iterated and each object of the queryset is saved with the fk changed
to point to the obj
. An implicit save.
Now, I don't like the implicit saves at all. A variable assignment
should not cause a database save. So, I would like to deprecate the
current behavior. Assignment to reverse_fk_set
(and I guess this goes
for m2m, too) is no longer allowed. Instead you will need to
explicitly do:
>>> obj.reverse_fk_set.set(RelatedObj.objects.all())
Now you are calling a method, and saving in this situation is OK and
analogous to other related manager methods.
Lets raise a deprecation warning on direct assignment to the
reverse_fk_set
and remove it. The message would be
something like "Direct assignment to the reverse side of a related set
is deprecated. Use .set() instead."
Change History (9)
comment:1 by , 9 years ago
Has patch: | set |
---|
comment:2 by , 9 years ago
Triage Stage: | Unreviewed → Accepted |
---|
PR