如何访问Django的field.choices? [英] How to access Django's field.choices?
本文介绍了如何访问Django的field.choices?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种方法(不使用表单)来访问模型字段的选择值?
Is there a way (without using a form) to access a model fields choices value?
我想做类似 field.choices
的事情,并在视图或模板中获取值列表.
I want to do something like field.choices
and get the list of values either in a view or template.
推荐答案
好的.只需访问模型"字段的 choices
属性:
Sure. Just access the choices
attribute of a Model field:
MyModel._meta.get_field('foo').choices
my_instance._meta.get_field('foo').choices
这篇关于如何访问Django的field.choices?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文