Skip to content

Instantly share code, notes, and snippets.

@darthwade
Last active August 29, 2015 14:04
Show Gist options
  • Save darthwade/05b3044a5b964a74549d to your computer and use it in GitHub Desktop.
Save darthwade/05b3044a5b964a74549d to your computer and use it in GitHub Desktop.
>>> # Grab countries from https://github.com/umpirsky/country-list/blob/master/country/cldr
>>> LANG = 'uk'
>>> FIELD_SUFFIX = '_uk'
>>> DOWNLOAD_URL = 'https://raw.githubusercontent.com/umpirsky/country-list/master/country/cldr/{lang}/country.txt'
>>> from urllib2 import urlopen
>>> lines = urlopen(DOWNLOAD_URL.format(lang=LANG)).read().split('\n')
>>> from apps.gis.models import Country
>>> for l in lines:
>>> if not len(l): break
>>> l = l.decode('utf-8')
>>> name = l[:-5]
>>> id = l[-3:-1]
>>> defaults = {'name' + FIELD_SUFFIX: name}
>>> (obj, created) = Country.objects.get_or_create(id=id, defaults=defaults)
>>> for k,v in defaults.items():
>>> setattr(obj, k, v)
>>> obj.save()
>>> print id, obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment