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

nothingness picture nothingness · Apr 28, 2016 · Viewed 36.2k times · Source

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

Answer

solarissmoke picture solarissmoke · Apr 28, 2016

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