Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base: master
...
compare: master
Checking mergeability… Don’t worry, you can still create the pull request.
  • 5 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 60 additions and 1 deletion.
  1. +8 −0 django_countries/countries.py
  2. +52 −1 django_countries/fields.py
View
8 django_countries/countries.py
@@ -765,3 +765,11 @@
'ZM': u'ZAMBIA',
'ZW': u'ZIMBABWE',
}
+
+def country_for_code(c):
+ c = c.upper()
+ c = 'GB' if c == 'UK' else c
+ for k,v in COUNTRIES:
+ if k == c:
+ return v
+ raise ValueError("%(code)s is not a valid country code" % { 'code': c})
View
53 django_countries/fields.py
@@ -1,3 +1,4 @@
+from django import forms
from django.db.models.fields import CharField
from django.utils.encoding import force_unicode, StrAndUnicode
from django_countries import settings
@@ -75,6 +76,34 @@ def __set__(self, instance, value):
instance.__dict__[self.field.name] = value
+class CountryFormField(forms.ChoiceField):
+ def __init__(self, *args, **kwargs):
+ # Local import so the countries aren't loaded unless they are needed.
+ from django_countries.countries import COUNTRIES
+ choices = COUNTRIES
+ ordered = kwargs.pop('ordered',None)
+ sort = kwargs.pop('sort',None)
+ if sort:
+ from operator import itemgetter
+ if sort == 'Name':
+ choices = sorted(choices, key = itemgetter(1))
+ else:
+ pass #right now choices is sorted by code already
+ if ordered:
+ choices_in_ordered = {}
+ ordered_choices = []
+ other_choices = []
+ for k,v in choices:
+ if k in ordered:
+ choices_in_ordered[k]=v
+ else:
+ other_choices.append((k,v))
+ for o in ordered:
+ ordered_choices.append((o,choices_in_ordered[o]))
+ choices = tuple(ordered_choices + other_choices)
+ super(CountryFormField, self).__init__(*args, **kwargs)
+ self.choices = choices
+
class CountryField(CharField):
"""
A country field for Django models that provides all ISO 3166-1 countries as
@@ -86,9 +115,31 @@ class CountryField(CharField):
def __init__(self, *args, **kwargs):
# Local import so the countries aren't loaded unless they are needed.
from django_countries.countries import COUNTRIES
+ choices = COUNTRIES
+ ordered = kwargs.pop('ordered',None)
+ sort = kwargs.pop('sort',None)
+ if sort:
+ from operator import itemgetter
+ if sort == 'Name':
+ choices = sorted(choices, key = itemgetter(1))
+ else:
+ pass #right now choices is sorted by code already
+ if ordered:
+ choices_in_ordered = {}
+ ordered_choices = []
+ other_choices = []
+ for k,v in choices:
+ if k in ordered:
+ choices_in_ordered[k]=v
+ else:
+ other_choices.append((k,v))
+ for o in ordered:
+ ordered_choices.append((o,choices_in_ordered[o]))
+ choices = tuple(ordered_choices + other_choices)
+
kwargs.setdefault('max_length', 2)
- kwargs.setdefault('choices', COUNTRIES)
+ kwargs.setdefault('choices', choices)
super(CharField, self).__init__(*args, **kwargs)

No commit comments for this range

Something went wrong with that request. Please try again.