Reputation: 350
I've been trying to get this to work and have failed. I've used - help_text, blank_label, default etc which have all resulted in errors. I have a form in which you choose your nationality. The model is working fine and in the admin section too. The countries are being generated using the Django-countries package. However when I render the form the default is Afghanistan. Now, I want to the default to be 'select a country' and then people choose from the drop down. The drop down itself is working beautifully. How do I do this? (PS- I've checked various questions here and they dont answer the question like: Django: Country drop down list?, set up form option as default in a drop down, django countries dropdown default etc)
as of 31/08/17 I even added Meta which changes the placeholder value. Whilst this has changed the value of other fields it has not affected the countries field. See below for updated code.
From form.py snippet
nationality = LazyTypedChoiceField(choices=countries, label='Nationality', required=False)
class Meta:
model = ProfileTeacher
fields = ('description', 'document' 'name','last_name','phone_number', 'date_of_birth', 'bank_account', 'id_number', 'postal_code', 'adress', 'profile_image', 'nationality' )
def __init__(self, *args, **kwargs):
super(RegisterFormTeacher, self).__init__(*args, **kwargs)
self.fields['nationality'].widget.attrs['placeholder'] = 'Select a Country'
from model.py
nationality = CountryField(blank_label='(select country)', null=True, blank = True)
Upvotes: 2
Views: 1346
Reputation: 350
It turns out to be rather simple. Within form.py I needed to change it from:
nationality = LazyTypedChoiceField(choices=countries, label='Nationality', required=False)
to
nationality = CountryField(blank_label='(Select country)',).formfield()
Within the .formfield() you can then set other attributes like 'required' etc.
Upvotes: 1