Let's say I have following ORM classes (fields removed to simplify):
class Animal(models.Model):
say = "?"
def say_something(self):
return self.say
class Cat(Animal):
self.say = "I'm a cat: miaow"
class Dog(Animal):
self.say = "I'm a dog: wuff"
class Animals(models.Model):
my_zoo = models.ManyToManyField("Animal")
When I add some animals to my zoo:
cat = Cat()
cat.save()
dog = Dog()
dog.save()
animals.my_zoo.add(cat)
animals.my_zoo.add(dog)
for animal in animals.my_zoo.all():
print animal.say_something()
... I would expect following result:
I'm a cat: miaow, I'm a dog: wuff
but instead, all I've got is the instances of general Animal object, unable to say anything but "?".
How to achieve the true object inheritance and later polymorphism when the object is retreived from db?