USStateField default value

54 views
Skip to first unread message

Erisa

unread,
Dec 31, 2011, 4:52:54 PM12/31/11
to Django users
I wanted to have a select box for the state with California as the
default. I first tried in my model the following:

state = USStateField(blank=True, default='CA')

This gave me the error "TypeError: __init__() got an unexpected
keyword argument 'default'".

But the following works (i.e., California is the default and I have a
nice select box):

state = models.CharField(blank=True, max_length=2,
choices=STATE_CHOICES, default='CA')

Could this be a bug? I am using 1.3.

Mengu

unread,
Dec 31, 2011, 8:46:25 PM12/31/11
to Django users
hi erisa,

please try passing "initial" instead of default.

Erisa

unread,
Jan 1, 2012, 3:32:29 PM1/1/12
to Django users
Changing 'default' to 'initial' gives me an unexpected argument
'blank' error. If I remove the blank=True attribute, there is no
error,
but the select box is absent from the form!

Martin Pajuste

unread,
Jan 2, 2012, 8:43:12 AM1/2/12
to django...@googlegroups.com
You get that error because 'blank' is model field option. If you want to be able to accept empty values, use required=False instead.
state = USStateField(initial='CA', required=False)
Reply all
Reply to author
Forward
0 new messages