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?
, , , - , , , . :
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)
, , .
? , , , IS-A.
Python , , , .
, , ( "If , , , ".) , , , . - () isinstance(). ( , , .) hasattr() EAFP.
, EAFP , :
, . Python , . . LBYL, , C.
. , ( ( -ORM-)), , . hasattr , .
, :
class Foo(models.Model, OurKitchenSinkClass):
MixIn. , .