开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜