On Sun, 6 Sep 2020 12:01:34 +0100
Adam Johnson <
m...@adamj.eu> wrote:
> I think it would be acceptable to make related name clashes a check
> Error. I'm guessing you couldn't find any justification for why the
> check doesn't currently do this?
>
I didn't find any, though I didn't look too hard.
On the other hand, I found that the behavior changes depending on the
target model being a proxy model (or rather, on the model defining the
property being a proxy model).
That is, with
class A(models.Model):
@property
def related(self):
return 15
class B(models.Model):
a = models.ForeignKey(A, related_name='related',
on_delete=models.CASCADE)
class C(A):
class Meta:
proxy = True
@property
def pro_related(self):
return 17
class D(models.Model):
c = models.ForeignKey(C, related_name='pro_related',
on_delete=models.CASCADE)
For instances a and c of A and C respectively,
a.related is a RelatedManager
c.pro_related is 17
This happens because reverse-accessors are promoted to the concrete
model -- the accessor attribute is set on A, which means it overrides
whatever was defined on A, but can be overridden by A's subclasses.
I believe the technical term of this is "a mess".
Shai.