My models are organized as follows.
class Animal(models.Model):
first_name = models.CharField(max_length=128, unique=True)
class Cat(Animal):
def make_sound(self):
return "mreooooow"
class Dog(Animal):
def make_sound(self):
return "AARF!"
class EvilMonkey(Animal):
def make_sound(self):
return "muahahaha ..."
As you see, the various animals inherit from a class called 'Animal.' Each subclass also has a method make_sound
.
However, in my view, when I call make_sound
on a generic animal, Django complains that Animal
objects have no method called make_sound
, which makes sense since only objects of subclasses of Animal
have the method.
Can I somehow tell Django that I can guarantee that all objects of subclasses of Animal
have the make_sound
method? Via something like a Java interface? I know that Django documentation said that "Field name "hiding" is not permitted," but I feel that overriding methods so fundamental to inheritance that Django should support it.
I don't want to have to be specific and call say my_animal.cat.make_sound()
since I want to treat the animal objects generically.