There is a fatal bug in OneToOneField? cacheing subsystem. Consider following models:
class A( models.Model ):
foo = models.IntegerField()
def __unicode__( self ):
return "%d" % self.foo
class B( A ):
a = models.OneToOneField( A , related_name = "b" , parent_link = True )
class C( A ):
a = models.OneToOneField( A , related_name = "c" , parent_link = True )
> B(foo=1).save()
> C(foo=2).save()
> A.objects.all()
[<A: 1>, <A: 2>]
> a = A.objects.filter( foo = 1 )[0]
> a.b
<B: 1>
> a.c
<B: 1>
In both cases (b and c fields pointing to different subclasses) is returns the same object, but in the second case an exception should be throwed.
The reason is cacheing subsystem in SingleRelatedObjectDescriptor? class of the module django.db.models.fields.related. In the constructor it creates twice cache with the same so called cache name: self.cache_name = '_%s_cache' % related.field.name because related.field.name points to the same string in both cases. In my opinion it should point to different strings. After aplying attached patch, which uses just related.name instead of related.field.name everything seems to work correctly (cache still works but separately for both fields).
> A.objects.all()
[<A: 1>, <A: 2>]
> a = A.objects.filter( foo = 1 )[0]
> a.b
<B: 1>
> a.c
<class 'megasite.test.models.DoesNotExist'>: C matching query does not exist.