Django CreateView字段标签 [英] Django CreateView field labels
问题描述
我正在一个有一个章节的项目中,每个章节都有标题,内容和顺序.我想按原样保留字段订单"的名称,但让字段在CreateView中显示为其他名称,例如章号".我发现的最好的信息是建议更新Meta类中的标签"属性,但这对我不起作用.
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'),
}
我也尝试过在Meta之外使用'label'属性,但这也不起作用.我应该改用ModelForm,还是有正确的方法呢?
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?
推荐答案
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'))
请注意,我在此示例中使用了IntegerField,请使用所需的任何类型.
Note I have use IntegerField in this example, please use whatever type is required.
这篇关于Django CreateView字段标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!