Assuming I have the following models:
class SomeSuperClass(models.Model):
...
class SomeSubClassA(SomeSuperClass)
...
class SomeSubClassB(SomeSuperClass)
...
class SomeConnector(models.Model):
reference = models.ForeignKey(SomeSuperClass, on_delete=models.CASCADE)
...
Now what I would want to have is somehow when iterating over objects of SomeConnector
I always want to have right away objects of the respective subclasses, not of the superclasses. E.g.
for sc in SomeConnector.objects.all():
# somehow get the correct subclass of this `reference` field here,
# assuming it to be callable under sc.reference_correct_subclass:
print(sc.reference_correct_subclass.__class__.__name__)
could produce for example:
'SomeSubClassA'
'SomeSubClassB'
'SomeSubClassA'
'SomeSubClassA'
But never should an object of the superclass be used.
I know of django-model-utils and I could do something similiar by querying directly on the superclass, like this:
SomeSuperClass.objects_inheritance.select_subclasses()
where objects_inheritance
is the InheritanceManager
attached to SomeSuperClass
. However I could not figure out yet how to reproduce this when the superclass is used as foreign key in another class which I want to use for querying.