reportingmonkey
reportingmonkey

Reputation: 185

django countries dropdown default

I am using Django ModelForms. I have a dropdown selector that allows users to select the country. The country is part of the booking model and is defined using django_countries. For example,

In models.py

from django_countries import CountryField

class Booking(models.Model)
    name = models.charField(max_length=100)
    country = CountryField()

In views.py I want to be able to set a default selected item in the drop down menu, for example 'United Kingdom'. I know how to do this with text fields, however cannot get it to work with the CountryField(). For example,

booking = BookingForm(initial={'name': 'mr majeika'}) //works
booking = BookingForm(initial={'country': 'United Kingdom'}) //does not work

I was hoping someone out there may be able to point me in the right direction?

Upvotes: 1

Views: 4740

Answers (1)

xysun
xysun

Reputation: 2015

According to here:

http://pypi.python.org/pypi/django-countries/1.0.1

"CountryField is based on Django's CharField, providing choices corresponding to the official ISO 3166-1 list of countries (with a default max_length of 2)"

And the complete list of the CountryField here:

http://djangosnippets.org/snippets/1476/

Can you try this and see if it works?

booking = BookingForm(initial={'country': 'GB'})

Upvotes: 4

Related Questions