python - How to invoke the right method in django inherited model -
i have model classes b , c implements method f , inherits model class a. want invoke function f on instances (b , c). why do
for in a.objects.all(): a.f()
doesn't work expected?
that's because default django manager doesn't manage polymorphism. when do
for in a.objects.all(): …
all a
objects of type a, , no type b or c.
you need use django-model-utils application, , it's select_subclasses
tool:
for in a.objects.select_subclasses(): # type(a) returns correct subclass
Comments
Post a Comment