以django形式显示特定字段 [英] Displaying a specific field in a django form

查看:165
本文介绍了以django形式显示特定字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个django形式的以下字段:

I have the following field in a django form:

position = forms.ModelChoiceField(Position.objects.order_by('-ordering'),
                                    empty_label='Select Position',)

在我的 Position model,我使用 unicode 字段显示名为position的字段。但是,在这种特定形式中,我希望输出是名为position-select的模型中的不同字段。如何更改 unicode 字段的默认输出?

In my Position model, I am using the unicode field to display the field called "position". However, in this particular form, I want the output to be a different field in the model called "position-select". How would I do this without altering the default output of the unicode field?

谢谢。

推荐答案

根据 https://docs.djangoproject.com/en/dev/ref/forms/fields/#modelchoicefield 。您可以指定对该重写类别中其他字段的 __ unicode __ 函数的引用。

Try "subclassing ModelChoiceField and override label_from_instance", per the example at https://docs.djangoproject.com/en/dev/ref/forms/fields/#modelchoicefield. You could specify a reference to the __unicode__ function of your other field within that overridden class.

这篇关于以django形式显示特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆