Reputation: 348
I know that is the simple question but I have trouble with this I have a table that shows colors and I used this as foreign key in Post table
class Post(models.Model):
"""docstring for Post"""
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1 )
slug = models.SlugField(unique=True)
post_title = models.CharField(max_length=50, help_text="write about your product")
color=models.ForeignKey(Color)
phone=models.CharField(max_length=20)
now when I want to show the form of Post
to show the first row like this in color field
now I want to show choose color instead of --------
.
Upvotes: 1
Views: 2196
Reputation: 19806
In your model form, you can use __init__()
to initialize your form. Something like:
from django import forms
from .models import Post
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ('post_title', 'color') # fields to show in your form
def __init__(self, *args, **kwargs):
super(PostForm, self).__init__(*args, **kwargs)
self.fields['color'].empty_label = "Choose color"
Upvotes: 2
Reputation: 1461
By default the widget used by ModelChoiceField will have an empty choice at the top of the list. You can change the text of this label (which is "---------" by default) with the empty_label attribute, or you can disable the empty label entirely by setting empty_label to None
Ex:
field1 = forms.ModelChoiceField(queryset=..., empty_label="(Choose color)")
Upvotes: 1