Is there any way to make Django's USStateField() to not have a pre-selected value?
django, django-forms, django-models
Solution
This seems to be a known issue (though I'm not aware of a ticket - I'd double-check there's not a ticket for it, and if not, file it):
from django.contrib.localflavor.us.us_states import STATE_CHOICES
from django.contrib.localflavor.us.forms import USStateField
class YourModelForm(forms.ModelForm):
class Meta:
...
YOUR_STATE_CHOICES = list(STATE_CHOICES)
YOUR_STATE_CHOICES.insert(0, ('', '---------'))
state = USStateField(widget=forms.Select(
choices=YOUR_STATE_CHOICES))
Above code from here.
Problem
I use USStateField() from Django's localflavor in one of my models: ``` class MyClass(models.Model): state = USStateField(blank=True) ``` Then I made a form from that class: ``` class MyClassForm(forms.ModelForm): class Meta: model = MyClass ``` When I display the form, the field "State" is a drop-down box with "Alabama" pre-selected. Is there any way to make the drop-down box to show no pre-selected value at all?