Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add a verbose_name in forms.py Django?

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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',
        }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading