Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

django - how can I change the modelform label and give it a custom name

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

question from:https://stackoverflow.com/questions/36905060/how-can-i-change-the-modelform-label-and-give-it-a-custom-name

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...