[Fixed]-Django form field dynamic queryset field

1👍

class EventForm(forms.Form):
    ...
    product = forms.ModelMultipleChoiceField(queryset=Product.objects.all())

    def __init__(self, *args, **kwargs):
        super(EventForm, self).__init__(*args, **kwargs)
        self.fields['product'].queryset = Product.objects.filter(company=company_id)
    # Where company_id is coming from either **kwargs or from the view.

Leave a comment