﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
28856	GenericForeignKey attributes create new instances on every access	Morgan Wahl	nobody	"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."	Bug	closed	contrib.contenttypes	1.10	Normal	fixed			Accepted	1	0	0	0	0	0
