如何在DjangModelUtils中获取选项的全名

2024-09-27 07:26:09 发布

您现在位置:Python中文网/ 问答频道 /正文

在向数据库添加新条目时,我使用django-model-utils选择一个选项。 我的模型是这样的:

class Book(models.Model):
   STATUS = Choices(
       ('available', _('Available to borrow')),
       ('borrowed', _('Borrowed by someone')),
   )
   status = models.CharField(
       max_length=32,
       choices=STATUS,
       default=STATUS.available,
   )

在我的管理页面中,它显示了一个选项的全名(可供借用),但是当我试图使用{{ book.status }}将此名称添加到HTML模板时,我得到的是available,而不是Available to borrow。我试过了,但没用。有什么想法吗,可以把显示文本拉到HTML模板上吗


Tags: todjango模板数据库modelmodelshtml选项
1条回答
网友
1楼 · 发布于 2024-09-27 07:26:09

您应该使用^{}函数,如文档所示

For every field that has choices set, the object will have a get_FOO_display() method, where FOO is the name of the field. This method returns the “human-readable” value of the field.

{{ book.get_status_display}}

相关问题 更多 >

    热门问题