我在django表单中有以下字段:
position = forms.ModelChoiceField(Position.objects.order_by('-ordering'),
empty_label='Select Position',)
在我的Position
模型中,我使用unicode
字段来显示名为“position”的字段。然而,在这个特定的表单中,我希望输出是模型中名为“position-select”的不同字段。如果不更改unicode
字段的默认输出,我该如何做到这一点?
- 谢谢-谢谢
3条答案
按热度按时间inb24sb21#
这是有效的方法:
guykilcj2#
根据www.example.com上的示例,尝试“子类化ModelChoiceField并覆盖label_from_instance”https://docs.djangoproject.com/en/dev/ref/forms/fields/#modelchoicefield。您可以在该覆盖类中指定对其他字段的
__unicode__
函数的引用。toiithl63#
在表单的init方法内的一行中: