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

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -