Django Inheritance: How to get one method for all subclasses?

I have a model

BaseModel

and several subclasses

ChildModelA(BaseModel), ChildModelB(BaseModel), ...

using multitask inheritance. In the future, I plan to have dozens of subclass models.

All subclasses have some method implementation.

do_something()

How can I call do_somthing from an instance of BaseModel?

An almost identical problem (without solution) is posted here:
http://peterbraden.co.uk/article/django-inheritance

A simpler question: how do I enable basModel instnace to one of its subclasses instances without checking all possible subclasses?

+5
source share
3 answers

, , , - , , , . :

def resolve(self):
    module, cls_name = self.class_name.rsplit(".",1)
    module = import_module(module)
    cls = getattr(module, cls_name)
    return cls.objects.get(pk=self.pk)

, , .

+2

? , , , IS-A.

Python , , , .

, , ( "If , , , ".) , , , . - () isinstance(). ( , , .) hasattr() EAFP.

, EAFP , :

, . Python , . . LBYL, , C.

+1

. , ( ( -ORM-)), , . hasattr , .

, :

class Foo(models.Model, OurKitchenSinkClass):

MixIn. , .

0
source

All Articles