Django ModelForms, override a field, keep the same label
In my ModelForm, I have to override some settings of the fields (e.g. choices
, or required
state). This requires declaring the entire field 开发者_如何学编程again as formfield.
Is there a simple way to access the verbose_name
of the model field, so this doesn't have to redefined?
You don't have to redefine the field to change these settings. You can access the field in the form __init__
like below.
class MyForm(forms.ModelForm):
class Meta(object):
model = MyModel
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.fields['my_field'].required = True
精彩评论