【问题标题】:how to pass parameters to forms.py如何将参数传递给forms.py
【发布时间】:2017-07-14 05:52:54
【问题描述】:

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

class car(forms.Form,email):

    cars = forms.ModelChoiceField(
        empty_label = "Current Cars",
        queryset = Cars.objects.order_by('name').filter(color=color),
        widget = Select(attrs={'class': 'span6 small-margin-top small-margin-bottom'}),
        required=True
    )

【问题讨论】:

标签: python django


【解决方案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)

【讨论】:

    猜你喜欢
    • 2020-01-25
    • 2019-04-23
    • 1970-01-01
    • 2020-10-09
    • 2012-12-21
    • 2012-01-07
    • 2014-05-03
    • 2015-06-26
    • 2012-01-18
    相关资源
    最近更新 更多