Custom Search

Tuesday, December 17, 2013

How to install django-cities-light and populate country region state and city dropdown

1)
pip install django-cities-light
pip install south

2)
Add "cities_light" to your INSTALLED_APPS.

3)
./manage.py syncdb



4)
Help
./manage.py help cities_light

5)
Populate tables
./manage.py cities_light

6)
#python manage.py shell
>>> import cities_light as cl
>>> cl.models.Country.objects.filter(name='india').all()
>>> cl.models.Region.objects.filter(name='kerala').all()
>>> cl.models.City.objects.filter(name='bangalore').all()

1 comment:

  1. Good job!

    I always get the following error after running python manage.py runserver:

    Saving Komsomolabadskiy Rayon, Tajikistan failed: IntegrityError('null value in column "region_id" violates not-null constraint\nDETAIL: Failing row contains (44833, Komsomolabadskiy Rayon, Komsomolabadskiy Rayon, komsomolabadskiy-rayon, 1221249, , Komsomolabadskiy Rayon, Tajikistan, 1221249, 220, null).\n',)
    Saving Gissarskiy Rayon, Tajikistan failed: IntegrityError('null value in column "region_id" violates not-null constraint\nDETAIL: Failing row contains (44834, Gissarskiy Rayon, Gissarskiy Rayon, gissarskiy-rayon, 1221707, , Gissarskiy Rayon, Tajikistan, 1221707, 220, null).\n',)
    Saving Gálvez, Santa Fe, Argentina failed: IntegrityError('duplicate key value violates unique constraint "cities_light_city_region_id_slug_dc18c213_uniq"\nDETAIL: Key (region_id, slug)=(121, galvez) already exists.\n',)
    Traceback (most recent call last):
    File "manage.py", line 21, in
    main()
    File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
    File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
    File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
    File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
    File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
    File "/app/.heroku/python/lib/python3.6/site-packages/cities_light/management/commands/cities_light.py", line 205, in handle
    self.city_import(items)
    File "/app/.heroku/python/lib/python3.6/site-packages/cities_light/management/commands/cities_light.py", line 504, in city_import
    items[ICity.admin2Code]
    File "/app/.heroku/python/lib/python3.6/site-packages/cities_light/management/commands/cities_light.py", line 282, in _get_subregion_id
    country_id=country_id, geoname_code=region_id).pk
    File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
    File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 417, in get
    self.model._meta.object_name
    cities_light.models.DoesNotExist: Region matching query does not exist.

    ReplyDelete