Say I have a set of models like this:
class Container(models.Model):
pass
class Parent(models.Model):
container = models.ForeignKey(Container, related_name='%(class)s_list')
class Child(Parent):
pass
Without making Parent
abstract, is it possible to make Child
's container
field have a related name of child_list
instead and all Child
's instances would be put in child_list
for the Container
model?
I've tried modifying Child
to have an additional field:
class Child(Parent):
container2 = models.ForeignKey(Container, related_name='%(class)s_list')
And then making the two fields be in sync'. However, since the real goal of this is to optimize a parent class with many inheritance-related children [1], using an aliased Container
key doesn't populate the cache correctly when doing a prefetch.
Any solution would probably cause an aliasing issue with Child
instance in list child_list
into sample_list
. However, I'm not intending to expose sample_list
and I'd be happy to work around aliasing.