有没有办法正确重载 Django 的 get_FIELD_display()
函数?当我从内部调用函数时,结果是递归。但我也不能使用 super()
调用它,因为它不是父类的方法,而是元类创建的方法...
目标是有一个通用接口(interface)来获取可显示版本的 CHOICE
字段(由 get_FIELD_display
给出),但可以在一些具体案例。
例子:
# This does not work because it results in recursion
def get_opposition_state_display(self):
"""Overloading of default function."""
value = self.get_opposition_state_display()
if self.opposition_state == 4:
return '%s %s' % (value, self.opposition_date.strftime('%d.%m.%Y'))
return value
最佳答案
已更新
field = self._meta.get_field('opposition_state')
value = self._get_FIELD_display(field)
关于django - 重载 get_FIELD_display() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9293294/