如何从Django中的ModelForm手动创建一个选择字段? [英] How to manually create a select field from a ModelForm in Django?
问题描述
我有一个 ModelForm
其中一个字段(名为创建者
)是一个 ForeignKey
,所以对于 {{form.creator}}
Django呈现< select>
标签如下:
I have a ModelForm
where one of the field (named creator
) is a ForeignKey
, so for {{ form.creator }}
Django renders the <select>
tag like this:
<select id="id_approver" name="approver">
<option selected="selected" value="">---------</option>
<option value="1">hobbes3</option>
<option value="2">tareqmd</option>
<option value="3">bob</option>
<option value="4">sam</option>
<option value="5">jane</option>
</select>
但是我想添加一个 onchange
事件属性,所以我以后可以使用AJAX做别的事情。我也想更改 ---------
来表示其他内容,并显示审批者的全名,而不是其用户名。
But I want to add a onchange
event attribute so I can use AJAX later to do something else. I also want to change ---------
to say something else and display the approvers' full name, not their username.
那么可以得到可能的批准者列表并生成我自己的选择选项?类似
So is it possible to get a list of possible approvers and generate my own select options? Kind of like
<select id="id_approver" name="approver" onchange="some_ajax_function()">
<option select="selected" value="0">Choose a user</option>
{% for approver in form.approver.all %} <!-- This won't work -->
<option value="{{ approver.pk }}">{{ approver.get_full_name }}</option>
{% endfor %}
</select>
而且,我也认为大多数审批者名单太大(比如50多个)那么我最终会想要一个可以查询的自动完成字段给审批者。所以我一定要写自己的HTML。
And I'm also thinking most of the list of approvers get too large (like over 50), then I'll eventually want some sort of a searchable auto-complete field for the approver. So then I'll definitely need to write my own HTML.
如果有人需要它,我的 ModelForm
像这样:
In case anyone needs it, my ModelForm
looks like this:
class OrderCreateForm( ModelForm ) :
class Meta :
model = Order
fields = (
'creator',
'approver',
'work_type',
'comment',
)
推荐答案
ModelChoiceField文档解释了如何做到这一点。
The ModelChoiceField documentation explains how to do this.
要更改空的标签: p>
To change the empty label:
empty_label
By default the <select> widget used by ModelChoiceField
will have an empty choice at the top of the list. You can change the text
of this label (which is "---------" by default) with the empty_label
attribute, or you can disable the empty label entirely by setting
empty_label to None:
# A custom empty label
field1 = forms.ModelChoiceField(queryset=..., empty_label="(Nothing)")
# No empty label
field2 = forms.ModelChoiceField(queryset=..., empty_label=None)
对于你的第二个查询,它也在文档中解释:
As for your second query, it is also explained the in docs:
The __unicode__ method of the model will be called to generate string
representations of the objects for use in the field's choices;
to provide customized representations, subclass ModelChoiceField and override
label_from_instance. This method will receive a model object, and should return
a string suitable for representing it. For example:
class MyModelChoiceField(ModelChoiceField):
def label_from_instance(self, obj):
return "My Object #%i" % obj.id
最后,要传递一些自定义ajax,请使用 attrs
参数为选择小部件(这是在ModelForm字段中使用什么)。
Finally, to pass some custom ajax, use the attrs
argument for the select widget (which is what is used in the ModelForm field).
最后你应该有这样的东西:
In the end, you should have something like this:
creator = MyCustomField(queryset=...,
empty_label="Please select",
widget=forms.Select(attrs={'onchange':'some_ajax_function()'})
这篇关于如何从Django中的ModelForm手动创建一个选择字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!