How to stop auto-capitalization of verbose_name in django

后端 未结 5 805
野趣味
野趣味 2021-01-03 06:45

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, v         


        
5条回答
  •  一整个雨季
    2021-01-03 07:09

    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)
    

提交回复
热议问题