python - Using a Django model method from a template -


i have model this:

class car(models.model):     parenttype = models.foreignkey("self", null=true,blank=true)     engine = models.textfield (null=true) 

a car can have engine or variant of car same engine. right have accessor this:

def getengine(self):     if self.parenttype:         return self.parenttype.getengine()     return self.engine 

but getting ugly templates, need property access (cannot call function) , end properties named same fields. there way express behaviour above directly within django?

if model method doesn't take arguments (beyond self), it'll work fine in template syntax (notice no parentheses)

{{ car.getengine }} 

the more general issue of overshadowing model field property/setter/getter more difficult , prone caveats, ref https://code.djangoproject.com/ticket/3148

when happens unless it's big deal might want use different property name, or template tag etc


Comments