Home:ALL Converter>django: call overridden model method from django template

django: call overridden model method from django template

Ask Time:2016-11-11T15:33:35         Author:Vipul

Json Formatter

I have overrode (overrided?) the get_FOO_display() method of django model class (see: doc reference) to return some different choice field mapping. But, on calling this function from the django template as {{ model_object.get_FOO_display }} calls the base class method instead of the new overridden method.

I read the source code django/db/models/base.py and it seems like this is not some regular method rather, it is created dynamically for the model fields. (let me know if am getting it wrong)

Is there a way I can resolve to the overridden method from the template ?

See this code to get the essence of my issue:

class UserStatus(models.Model):
    invite_status = models.CharField(max_length=15, choices=USER_STATUS, default='new')

    # Overridden method
    def get_invite_status_display(self):
        return dict(USER_STATUS_DISPLAY).get(self.invite_status)

Yes, I need to display different content than the choices dict and no, I cannot keep my to-be-displayed content in the choice dict in the first place.

Let me know if there is any possible way to make this template call work.

Thanks.

Author:Vipul,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40543208/django-call-overridden-model-method-from-django-template
yy