Hi
So I’m trying to add a class atrribute to this field on my ModelForm to allow for Bootstrap styling:
category = forms.ModelChoiceField(queryset=Category.objects.all())
I tried this:
category = forms.ModelChoiceField(queryset=Category.objects.all(), widget=forms.ModelChoiceField(attrs={'class':'form-control'}))
and got this error:
TypeError: __init__() missing 1 required positional argument: 'queryset'
How do I solve this?
You’re getting the error because you’re trying to add forms.ModelChoiceField as a widget into a forms.ModelChoiceField instance. In this case, I think you’re looking for forms.Select or forms.CheckboxInput. From there you should be able to add the widget attribute 
Here’s the documentation as a second resource just in case I’m wrong –
Hope this helps!
Thank you Suttonium. I managed to find the answer and this worked for me:
category = forms.ModelChoiceField(queryset=Category.objects.all(),)
category.widget.attrs.update({'class': 'form-control'})
or someone else did say I could try this as well:
category = forms.ModelChoiceField(
queryset=Category.objects.all(),
widget=forms.Select(attrs={'class': 'form-control'})
)
I imagine the code immediately above is along the lines of what you were trying to recommend.
You’re correct! The latter choice is what I was referring to. You can also edit the widget attribute in the Meta class on your form (which is what the former choice is referring to, just not in a “django-esque” way). Both are valid options if they work for you 