Django: how to hide/overwrite default label with ModelForm? Django: how to hide/overwrite default label with ModelForm? django django

Django: how to hide/overwrite default label with ModelForm?


To expand on my comment above, there isn't a TextField for forms. That's what your TextField error is telling you. There's no point worrying about the label until you have a valid form field.

The solution is to use forms.CharField instead, with a Textarea widget. You could use the model form widgets option, but it's simpler to set the widget when defining the field.

Once you have a valid field, you already know how to set a blank label: just use the label='' in your field definition.

# I prefer to importing django.forms# but import the fields etc individually# if you prefer from django import formsclass BooklogForm(forms.ModelForm):    book_comment = forms.CharField(widget=forms.Textarea, label='')    class Meta:         model = Booklog        exclude = ('Author',)


If you're using Django 1.6+ a number of new overrides were added to the meta class of ModelForm, including labels and field_classes.

See: https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/#overriding-the-default-fields


To override just the label you can do

def __init__(self, *args, **kwargs):     super(ModelForm, self).__init__(*args, **kwargs)    self.fields['my_field_name'].label = 'My New Title'