Home > Software design >  How to add a verbose_name in forms.py Django?
How to add a verbose_name in forms.py Django?

Time:05-25

class ApplicationForm(BaseForm):
    class Meta:
        model = Application
        fields = ['referencenumber', 'name', 'description', 'owner']

I have the above form from models.py. However I want to put labels on the form that are different than the verbose_name of models.py. I can't edit models.py since we are too far into development.

Any way to do this in forms? Please help!

CodePudding user response:

Labels are verbose_names of the model you can change it. This will help you:

Solution #1

class ApplicationForm(BaseForm):
    class Meta:
        model = Application
        fields = ['referencenumber', 'name', 'description', 'owner']

    def __init__(self, *args, **kwargs):
        super(ApplicationForm, self).__init__(*args, **kwargs)
        self.fields['referencenumber'].label = "reference number"
        self.fields['name'].label = "name"

Solution #2

class ApplicationForm(BaseForm):
    class Meta:
        model = Application
        fields = ['referencenumber', 'name', 'description', 'owner']
        labels = {
            'referencenumber': 'referencenumber',
            'name': 'name',
        }
  • Related