0

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!

renkse
  • 502
  • 7
  • 15

2 Answers2

1

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".

minusf
  • 1,204
  • 10
  • 10
  • So, an answer to the point 2) should be something like "use `if hasattr(widget, 'input_type')`", i suppose.. unless of course they are going to add input_type in the future. Thank you for information anyway. – renkse Apr 02 '19 at 09:21
1

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':
Pol Clota
  • 101
  • 1
  • 1