[Answer]-How do I allow for overridable methods via Django's multi-table inheritance?

1đź‘Ť

The trouble isn’t that Animal doesn’t know about make_sound. It’s simply that when you ask for an Animal in Django, that’s what you get: even if that Animal is “actually” a subclass rather than the base class, only the base instance will be returned. That’s because Django doesn’t attempt to do the query to find if there’s a related item in any of the subclassed tables – which is probably on balance a good thing, since you would have many many unnecessary queries.

The only thing to do is to record what type of Animal it is on the base class, then write a method to get the relevant subclass from that.

Leave a comment