Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop auto-capitalization of verbose_name in django

How to prevent Django from auto-capitalizing of the verbose_name in models? E.g:

class TestModel(models.Model):
    enb_id = models.IntegerField(null=True, verbose_name="eNB ID", blank=True)

I want to handle the capitalization myself and display "eNB ID" instead of "ENB ID" anywhere on the site.

like image 919
Mariusz Jamro Avatar asked Dec 04 '22 05:12

Mariusz Jamro


1 Answers

It seems that Django capitalizes the first letter when setting the form field for that model field:

...
defaults = {
    'required': not self.blank,
    'label': capfirst(self.verbose_name),
    'help_text': self.help_text
}

You could create your own custom model field that overwrites the capfirst (by passing the label as kwarg):

from django.db import models
class UpcappedModelField(models.Field):

    def formfield(self, form_class=forms.CharField, **kwargs):
        return super(UpcappedModelField, self).formfield(form_class=forms.CharField, 
                         label=self.verbose_name, **kwargs)
like image 117
Timmy O'Mahony Avatar answered Feb 19 '23 18:02

Timmy O'Mahony