renkse
renkse

Reputation: 542

Django textarea widget does not have attribute "input_type"

I'm trying to check input type of widget as below:

for field in form:
    if field.field.widget.input_type == 'checkbox':
        do_smth()
    else:
        do_smth_else()

but it seems Django Textarea widget does not have attribute "input_type".

I already solved the problem by wrapping input_type check in try/except block:

try:
    input_type = field.field.widget.input_type
except AttributeError:
    input_type = 'textarea'

but I have 2 questions:

1) Why only this widget does not have "input_type", and others have?

2) Is there a better way to solve the problem above?

I'm sorry for my English and thank you for advance!

Upvotes: 1

Views: 984

Answers (2)

Pol Clota
Pol Clota

Reputation: 161

My approach. Make sure sequence is as shown otherwise you'll get an error...

from django.forms.widgets import Textarea

if isinstance(field.widget, Textarea) or field.widget.input_type == 'text':

Upvotes: 1

minusf
minusf

Reputation: 1373

This is not really an answer, not a satisfying one in any case: https://code.djangoproject.com/ticket/30306

In a nutshell: "we are not adding it".

Upvotes: 1

Related Questions