如何将参数传递给forms.py

时间:2017-02-23 16:18:42

标签: python django

我在表单上有一个选择字段我试图在下拉列表中显示所有相同颜色的汽车,所选颜色由用户输入字段定义

{{1}}

1 个答案:

答案 0 :(得分:1)

Posible duplicated question

class AccountDetailsForm(forms.Form):
    ...
    adminuser = forms.ModelChoiceField(queryset=User.objects.all())
    def __init__(self, *args, **kwargs):
        accountid = kwargs.pop('accountid', None)
        super(AccountDetailsForm, self).__init__(*args, **kwargs)

        if accountid:
            self.fields['adminuser'].queryset = User.objects.filter(account=accountid)

form = AccountDetailsForm(accountid=3)