python - Django / python : How to cast an integer into the equivalent enum string?

标签 python django

<分区>

Possible Duplicate:
Django print choices value

在 Django 中的一个模型中,我有以下枚举:

PRIORITY = (
        ('2',     _(u'High')),
        ('1',   _(u'Medium')),
        ('0',      _(u'Low')),
    )

priority = models.CharField(max_length=1, choices=PRIORITY, default='1')

将优先级发送到模板时,该值仍然是整数,这不太好。我想用文字而不是数字来显示优先级。

context = Context({'priority':self.priority})

有没有办法在将优先级发送到模板之前不使用任何 if 语句将优先级转换为实际字符串?

最佳答案

是的,根据 the documentation您可以获得这样的人类可读值:

context.get_priority_display()

关于python - Django / python : How to cast an integer into the equivalent enum string?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13661427/

相关文章:

python - Pandas 比较两个数据帧

用于抓取 html 输出的 Python 脚本有时有效,有时无效

python - basemap 上的国家标签

javascript - 在客户端 Javascript 中调用 Django `reverse`

python - 替换 SQL 查询中的值

javascript - 使用 Wea​​syprint 生成 pdf 文件,保存为 zip 文件,将该 zip 文件发送给客户端并提供下载

python - 如何为开发者博​​客做一个更好的 Markdown

python - Pyramid 设置 mako.imports 不工作

python - Django 作为 Tornado 应用程序中的中间件

python - 复杂的 Django 查询,包括一对一模型