我怎样才能改变模型标签并给它一个自定义名称
how can I change the modelform label and give it a custom name
我想为模型中的一个标签创建自定义名称
这是我的 forms.py
class PostForm(forms.ModelForm):
body = forms.CharField(widget=PagedownWidget)
publish = forms.DateField(
widget=forms.SelectDateWidget,
initial=datetime.date.today,
)
class Meta:
model = Post
fields = [
"title",
"body",
"author",
"image",
"image_url",
"video_path",
"video",
"publish",
"tags",
"status"
]
我想更改而不是视频我希望它说嵌入。我检查了文档,但没有找到任何可以帮助我做到这一点的东西。有没有可能我不必重新排列我的模型?如果是这样怎么办?谢谢
是的,you can。只需使用 label
参数:
class PostForm(forms.ModelForm):
...
video = forms.FileField(label='embed')
或在您的 Meta
class:
中定义它
class PostForm(forms.ModelForm):
...
class Meta:
...
labels = {
"video": "embed"
...
}
You can specify the labels, help_texts and error_messages attributes of the inner Meta class if you want to further customize a field.
文档的该部分下方有示例。所以,你可以这样做:
class Meta:
model = Post
labels = {
"video": "Embed"
}
在不编辑表格的情况下实现此目的的一种简单方法是 change the verbose_name
在模型上。对于模型上的 video
字段,您可以将表单上的标签从 "video" 更改为 "embed",如下所示:
class Post(models.Model)
video = models.UrlField(verbose_name="embed")
# Other fields
class Meta:
model = Book
fields = ('title', 'publication_date', 'author', 'price', 'pages','book_type',)
labels = {
'title':'Titulo',
'publication_date':'Data de Publicação',
'author':'Autor',
'price':'Preço',
'pages':'Número de Páginas',
'book_type':'Formato'
}
widgets = {
'title': forms.TextInput(attrs={'class':'form-control'}),
'publication_date': forms.TextInput(attrs={'class':'form-control'}),
'author': forms.TextInput(attrs={'class':'form-control'}),
'price': forms.TextInput(attrs={'class':'form-control'}),
'pages': forms.TextInput(attrs={'class':'form-control'}),
'book_type': forms.TextInput(attrs={'class':'form-control'}),
}
我想为模型中的一个标签创建自定义名称 这是我的 forms.py
class PostForm(forms.ModelForm):
body = forms.CharField(widget=PagedownWidget)
publish = forms.DateField(
widget=forms.SelectDateWidget,
initial=datetime.date.today,
)
class Meta:
model = Post
fields = [
"title",
"body",
"author",
"image",
"image_url",
"video_path",
"video",
"publish",
"tags",
"status"
]
我想更改而不是视频我希望它说嵌入。我检查了文档,但没有找到任何可以帮助我做到这一点的东西。有没有可能我不必重新排列我的模型?如果是这样怎么办?谢谢
是的,you can。只需使用 label
参数:
class PostForm(forms.ModelForm):
...
video = forms.FileField(label='embed')
或在您的 Meta
class:
class PostForm(forms.ModelForm):
...
class Meta:
...
labels = {
"video": "embed"
...
}
You can specify the labels, help_texts and error_messages attributes of the inner Meta class if you want to further customize a field.
文档的该部分下方有示例。所以,你可以这样做:
class Meta:
model = Post
labels = {
"video": "Embed"
}
在不编辑表格的情况下实现此目的的一种简单方法是 change the verbose_name
在模型上。对于模型上的 video
字段,您可以将表单上的标签从 "video" 更改为 "embed",如下所示:
class Post(models.Model)
video = models.UrlField(verbose_name="embed")
# Other fields
class Meta:
model = Book
fields = ('title', 'publication_date', 'author', 'price', 'pages','book_type',)
labels = {
'title':'Titulo',
'publication_date':'Data de Publicação',
'author':'Autor',
'price':'Preço',
'pages':'Número de Páginas',
'book_type':'Formato'
}
widgets = {
'title': forms.TextInput(attrs={'class':'form-control'}),
'publication_date': forms.TextInput(attrs={'class':'form-control'}),
'author': forms.TextInput(attrs={'class':'form-control'}),
'price': forms.TextInput(attrs={'class':'form-control'}),
'pages': forms.TextInput(attrs={'class':'form-control'}),
'book_type': forms.TextInput(attrs={'class':'form-control'}),
}