如何更改模型标签并给它一个自定义名称 [英] how can I change the modelform label and give it a custom name

查看:123
本文介绍了如何更改模型标签并给它一个自定义名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的modelform中的标签创建一个自定义名称
这是我的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
标签,
状态
]

想改变而不是视频我想要它说嵌入。我检查了文档,但没有找到任何有助于我这样做的东西。是否可能没有我不得不重新排列我的模型?如果是这样的话谢谢

解决方案

文档


您可以指定标签 help_texts error_messages 属性,如果要进一步自定义字段。


在文档的这一部分下面有例子。所以,你可以这样做:

  class Meta:
model = Post
labels = {
video:Embed
}


I want to create a custom name for on of the labels in my modelform this is my 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"
         ]

I want to change the instead of video I want it to say embed. I checked the documentation but didn't find anything that would help me do that. is it possible without me having to rearrange my model? if so how? thanks

解决方案

From the documentation:

You can specify the labels, help_texts and error_messages attributes of the inner Meta class if you want to further customize a field.

There are examples just below that section of the docs. So, you can do:

class Meta:
    model = Post
    labels = {
        "video": "Embed"
    }

这篇关于如何更改模型标签并给它一个自定义名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆