Opened 7 years ago
Last modified 7 years ago
#28856 closed Bug
GenericForeignKey attributes create new instances on every access — at Version 1
Reported by: | Morgan Wahl | Owned by: | nobody |
---|---|---|---|
Component: | contrib.contenttypes | Version: | 1.10 |
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 (last modified by )
Given these models:
class OtherSuper(models.Model): pass class OtherSub(OtherSuper): pass class Ref(models.Model): obj_type = models.ForeignKey(ContentType, on_delete=models.PROTECT) obj_id = models.CharField(max_length=255) obj = GenericForeignKey('obj_type', 'obj_id')
I get this behavior:
In [1]: ref = Ref.objects.create(obj=OtherSub.objects.create()) In [2]: id(ref.obj) == id(ref.obj) Out[2]: True In [3]: ref.refresh_from_db() In [4]: id(ref.obj) == id(ref.obj) Out[4]: False
Each time ref.obj
is accessed, a new instance is created for its value. This is a problem, since doing something like ref.obj.field = 1; ref.obj.save()
won't actually update the field in the database. This only happens when the referenced object is an instance of a model that subclasses another model. (So, it wouldn't happen if referencing OtherSuper
in the models above.) The refresh_from_db()
call is also necessary to reproduce this in a simple test like the above; it happens with any instance created from an existing DB record.
I've written a regression test against stable/1.10.x . I'll attach a patch.
I discovered this because I have code that does the above (changes a field on the related model and calls save). I call this a regression because it works correctly on 1.9.
I'm not sure what the underly bug is; I looked at the diff in contenttypes
between 1.9 and 1.10, and there are more than a few changes. Hopefully someone who understands the GenericForeignKey
implementation can figure this out.
Change History (2)
by , 7 years ago
Attachment: | 1.10-regression-test.patch added |
---|
comment:1 by , 7 years ago
Description: | modified (diff) |
---|
patch with regression test