djangodjango-generic-views

Django CreateView field labels


I'm working on a project that has a Chapter, with each Chapter having a title, content, and order. I'd like to keep the field 'order' named as is, but have the field displayed in a CreateView as something else, like 'Chapter number'. The best information I've found recommends updating the "labels" attribute in the Meta class, but this isn't working for me.

This is what I'm using now, which doesn't work:

class ChapterCreate(CreateView):
    model = models.Chapter
    fields = [
        'title',
        'content',
        'order',
    ]

    class Meta:
        labels = {
            'order': _('Chapter number'),
        }

I've also tried using the 'label's attribute outside of Meta, but that didn't work either. Should I be using a ModelForm instead, or is there a correct way to do this?


Solution

  • The simplest solution in this case would be to set the verbose_name for your model field

    class Chapter(models.Model):
        order = models.IntegerField(verbose_name= _('Chapter number'))
    

    Note I have use IntegerField in this example, please use whatever type is required.