Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 967
nothingness Avatar asked Apr 28 '16 04:04

nothingness


People also ask

How do you exclude a specific field from a ModelForm?

Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.

What is ModelForm in Django?

Django Model Form It is a class which is used to create an HTML form by using the Model. It is an efficient way to create a form without writing HTML code. Django automatically does it for us to reduce the application development time.

What is label in Django?

label is used to change the display name of the field. label accepts as input a string which is new name of field. The default label for a Field is generated from the field name by converting all underscores to spaces and upper-casing the first letter.


2 Answers

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"     } 
like image 199
solarissmoke Avatar answered Sep 22 '22 18:09

solarissmoke


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"             ...         } 
like image 24
Selcuk Avatar answered Sep 20 '22 18:09

Selcuk