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

4

46

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

Ovoviviparous answered 28/4, 2016 at 4:7 Comment(0)
R
102

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"
    }
Rozella answered 28/4, 2016 at 4:11 Comment(3)
saved my day, pal! I wanted to mess with db column names xD or at least with model field names xDNavel
note: if you are translating the label, this should be done in the init method of the form: self.fields['video'].label = _('Embed'). This captures current language at runtime, whereas doing it in Meta makes the translation at startup in default lang (thus will be wrong for users of non-default lang).Superlative
I doubt that's the best way @Sean. You need to use the lazy form of gettext though, i.e. from django.utils.translation import gettext_lazy as _Vernation
M
28

Yes, you can. Simply use the label argument:

class PostForm(forms.ModelForm):
    ...
    video = forms.FileField(label='embed')

or define it inside your Meta class:

class PostForm(forms.ModelForm):
    ...
    class Meta:
        ...
        labels = {
            "video": "embed"
            ...
        }
Much answered 28/4, 2016 at 4:11 Comment(0)
K
7
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'}),
    } 
Knifeedged answered 26/7, 2018 at 0:18 Comment(0)
D
5

An easy way to achieve this without editing the form would be to change the verbose_name on the model. For the video field on your model you could change the label on the form from "video" to "embed" like so:

class Post(models.Model)
    video = models.UrlField(verbose_name="embed")
    # Other fields
Defiant answered 24/1, 2018 at 21:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.