[Django]-How do you get the type of widget used by a Django form field?

4👍

to get the the type of the widget in your code use type(field.field.widget)

BoundField is defined in django.forms.

👤akonsu

0👍

>>> from myapp.forms import RestaurantReviewForm
>>> f = RestaurantReviewForm()
>>> from django.forms.widgets import TextInput
>>> for field in f: print type(field.field.widget), isinstance(field.field.widget, TextInput)
... 
<class 'django.forms.widgets.TextInput'> True
<class 'django.forms.widgets.TextInput'> True
<class 'django.forms.widgets.TextInput'> True
<class 'django.forms.widgets.TextInput'> True
<class 'django.forms.widgets.TextInput'> True
<class 'django.forms.widgets.CheckboxInput'> False
<class 'django.forms.widgets.Select'> False
<class 'django.forms.widgets.Textarea'> False
<class 'captcha.fields.CaptchaTextInput'> False

Leave a comment