Percy3872
Percy3872

Reputation: 17

how to pass parameters to forms.py

I have a choice field on a form I am trying to display all the cars in a drop down list that are the same color, the color chosen is defined by a user input field

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
    )

Upvotes: 0

Views: 266

Answers (1)

alfredo138923
alfredo138923

Reputation: 1559

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)

Upvotes: 1

Related Questions