How do I assign a class to a field type in Django?

1

I'm trying to find out which field is a DateField and thereby assign a class='date' . How do I get the field type?

The following code does not work but displays the logic:

class MeuForm(forms.ModelForm):
    ...
    def __init__(self, *args, **kwargs):
        super(MeuForm, self).__init__(*args, **kwargs)            
        for field_name, field in self.fields.items():                
            if field == "DateField": # (exemplo) como saber que é um DateField?
                field.widget.attrs['class'] = 'date'
    
asked by anonymous 21.03.2015 / 20:52

1 answer

2

Use isinstance :

for field_name, field in self.fields.items():                
    if isinstance(field, forms.DateField):
        field.widget.attrs['class'] = 'date'
    
21.03.2015 / 21:12