使用
ModelChoiceField而不是简单的
ChoiceField:
user = forms.ModelChoiceField(queryset=User.objects.all(), empty_label="(Choose a User)")
更新 :您可以在窗体的构造函数中更改查询集。例如,如果您要从表单中排除已经添加的成员:
class AddMemberForm(Form): ... def __init__(self, *args, **kwargs): station = kwargs.pop('station') super(AddMemberForm, self).__init__(*args, **kwargs) if station: self.fields['user'].queryset = User.objects.exclude( id__in=station.members.all())然后使用
station参数创建表单:
form1 = AddMemberForm(station=station)



