I have the following setting:
class Baguette(models.Model):
# fields
class Pizza(models.Model):
# fields
class Topping(models.Model):
# fields
@classmethod
def do_something(cls):
# stuff
class Meta:
abstract = True
class PizzaTopping(models.Model):
obj = models.ForeignKey(Pizza, related_name='topping_set')
class BaguetteTopping(models.Model):
obj = models.ForeignKey(Baguette, related_name='topping_set')
I know, the way the relations are made is not perfect, but this is now the way it is.
My question is: Can I access do_something in a dynamic way via RelatedManager without knowing if there is PizzaTopping or BaguetteTopping behind it?
I have an instance obj of Pizza or Baguette (at this point I don’t know if it is Pizza or Baguette). Now I want to call something like this:
obj.topping_set.__modelclass__.do_something()
Is there a way to do this without testing the class of obj ?
obj.topping_set.modelwill be your model class. Soobj.topping_set.model.do_something().