フィールドのModelForm
1 つ ( という名前creator
) が であるForeignKey
ため、{{ form.creator }}
Django の場合、<select>
タグは次のようにレンダリングされます。
<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 を使用して別のことを実行できるようにしたいと考えています。---------
また、別のことを言い、承認者のユーザー名ではなくフルネームを表示するように変更したいと考えています。
可能な承認者のリストを取得して、独自の選択オプションを生成することは可能ですか? みたいな
<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 を作成する必要があります。
誰かがそれを必要とする場合に備えて、私のModelForm
見た目は次のようになります。
class OrderCreateForm( ModelForm ) :
class Meta :
model = Order
fields = (
'creator',
'approver',
'work_type',
'comment',
)