# How do I add a class attribute to a field on ModelForm

**URL:** <https://forum.djangoproject.com/t/how-do-i-add-a-class-attribute-to-a-field-on-modelform/4749>\
**Category:** Using Django\
**Created:** [October 12, 2020, 10:05am UTC](https://forum.djangoproject.com/t/how-do-i-add-a-class-attribute-to-a-field-on-modelform/4749 "2020-10-12T10:05:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SarahCAma](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/sarahcama/32/2425_2.png) [@SarahCAma](https://forum.djangoproject.com/u/SarahCAma)\
**Post date:** [October 12, 2020, 10:05am UTC](https://forum.djangoproject.com/t/how-do-i-add-a-class-attribute-to-a-field-on-modelform/4749/1 "2020-10-12T10:05:46Z")

</div>

Hi  
So I’m trying to add a class atrribute to this field on my ModelForm to allow for Bootstrap styling:

```auto
category = forms.ModelChoiceField(queryset=Category.objects.all())

```

I tried this:

```auto
category = forms.ModelChoiceField(queryset=Category.objects.all(), widget=forms.ModelChoiceField(attrs={'class':'form-control'}))

```

and got this error:

```auto
TypeError: __init__ () missing 1 required positional argument: 'queryset'

```

How do I solve this?

---

<div class="post-metadata">

**Author:** ![Suttonium](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/suttonium/32/1264_2.png) [@Suttonium](https://forum.djangoproject.com/u/Suttonium)\
**Post date:** [October 12, 2020, 12:58pm UTC](https://forum.djangoproject.com/t/how-do-i-add-a-class-attribute-to-a-field-on-modelform/4749/2 "2020-10-12T12:58:24Z")

</div>

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 –

> **[Widgets | Django documentation | Django](https://docs.djangoproject.com/en/3.1/ref/forms/widgets/)**

Hope this helps!

---

<div class="post-metadata">

**Author:** ![SarahCAma](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/sarahcama/32/2425_2.png) [@SarahCAma](https://forum.djangoproject.com/u/SarahCAma)\
**Post date:** [October 12, 2020, 3:55pm UTC](https://forum.djangoproject.com/t/how-do-i-add-a-class-attribute-to-a-field-on-modelform/4749/3 "2020-10-12T15:55:20Z")

</div>

Thank you Suttonium. I managed to find the answer and this worked for me:

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Suttonium](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/suttonium/32/1264_2.png) [@Suttonium](https://forum.djangoproject.com/u/Suttonium)\
**Post date:** [October 12, 2020, 4:26pm UTC](https://forum.djangoproject.com/t/how-do-i-add-a-class-attribute-to-a-field-on-modelform/4749/4 "2020-10-12T16:26:15Z")

</div>

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 🙂
