0

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
    )
Percy3872
  • 17
  • 7
  • Possible duplicate of [Django ModelChoiceField: filtering query set and setting default value as an object](http://stackoverflow.com/questions/5329586/django-modelchoicefield-filtering-query-set-and-setting-default-value-as-an-obj) – Max Feb 23 '17 at 19:49

1 Answers1

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)
Community
  • 1
  • 1
alfredo138923
  • 1,509
  • 1
  • 15
  • 15