我已经在我的模型中覆盖了_get_FIELD_display
,它在序列化器中正确使用
然而,Django管理员显然不把它叫做
模型class X(TimeStampedModel):
STATUSES = Choices(
("PENDING", _("Pending")),
("PLACED", _("Placed")),
("SHIPPED", _("Shipped")),
("DELIVERED", _("Delivered")),
)
status = FSMField(default=STATUSES.PENDING, choices=STATUSES, protected=True)
def get_status_display(self) -> str:
""" Overrides to replace Order.STATUSES.DELIVERED with Completed as it's confusing."""
value = self.status
if value == Order.STATUSES.DELIVERED:
return _("Completed!")
choices_dict = dict(Order.STATUSES)
# force_str() to coerce lazy strings.
return force_str(choices_dict[value], strings_only=True)
它不起作用,你可以检查这个方法链接。如果你读到这个方法这里有一些注释,比如
if self.choices is not None:
# Don't override a get_FOO_display() method defined explicitly on
# this class, but don't check methods derived from inheritance, to
# allow overriding inherited choices. For more complex inheritance
# structures users should override contribute_to_class().
if "get_%s_display" % self.name not in cls.__dict__:
setattr(
cls,
"get_%s_display" % self.name,
partialmethod(cls._get_FIELD_display, field=self),
)
所以如果你想重写这个方法你应该重写contribute_to_class首先在您使用的字段中,因此只需从FSMField创建一个子类并重写该方法