Reputation: 361
In my web application, i have a drop-down list created using ModelForm which is rendering well, however, i can't set default text like "Choose One Option" or "Select One", rather its showing the default "----" which I would love to override
I have tried using forms.ChoiceField in my widgets but its still not making any difference
from django import forms
from . import models
from .models import FacultyData
class DepartmentCreationForm(forms.ModelForm):
class Meta:
model = models.DepartmentData
fields = ['fid', 'dept_name']
data = []
#data.append((None, 'select one'))
data.append(('20', "---Choose One---"))
CHOICES = FacultyData.objects.all()
for v in CHOICES:
# fname = "%s -- $%d each" % (v.faculty_name, v.created_on)
data.append((v.id, v.faculty_name))
widgets = {
'fid': forms.Select(attrs={'class': 'form-control'}),
'dept_name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Enter Department Name'})
}
I expect the default output to be "Select One" but the actual output is "-----"
Upvotes: 0
Views: 2080
Reputation: 1
This works for me, hope it helps:
class yourform(forms.ModelForm):
##something##
def __init__(self, *args, **kwargs):
self.fields["fieldname"].empty_label="fieldDefaulText"
Upvotes: 0
Reputation: 11
Try something like this in your model:
options = (
('','Select One'),
('One','One'),
('Two','Two')
)
choice = models.CharField(max_length=100, choices=options)
Upvotes: 1
Reputation: 465
Try this :
class YourForm(forms.ModelForm):
your_field_name = forms.ModelChoiceField(queryset=FacultyData.objects.all(),empty_label="Select One")
class Meta:
model = DepartmentData
fields = ("__all__")
widgets = { something }
If it dont work show your model
Upvotes: 3