Skip to content
Browse files

Merge pull request #30 from Kometes/master

Postal code fix
  • Loading branch information...
2 parents 02fc747 + a263890 commit e102b569746afe9b4484bd0abe2556d0d0917999 @coderholic committed Feb 22, 2012
Showing with 57 additions and 48 deletions.
  1. +1 −1 MANIFEST.in
  2. +53 −33 README → README.md
  3. +0 −11 cities/management/commands/cities.py
  4. +1 −1 cities/models.py
  5. +2 −2 setup.py
View
2 MANIFEST.in
@@ -1,2 +1,2 @@
-include README LICENSE
+include README.md LICENSE
recursive-include cities *.py
View
86 README → README.md
@@ -1,70 +1,89 @@
- django-cities - Place models and data for Django apps
-=======================================================
+ django-cities -- *Place models and data for Django apps*
+=========================================================
This add-on provides models and commands to import country/region/city data into your database.
-The data is pulled from GeoNames and contains:
- - localized names
- - geographical codes
- - postal codes
- - geo-coords
- - population
+The data is pulled from [GeoNames](http://www.geonames.org/) and contains:
-Your database must support spatial queries, see GeoDjango for setup.
+ - localized names
+ - geographical codes
+ - postal codes
+ - geo-coords
+ - population
-For more information see:
-http://www.coderholic.com/django-cities-countries-regions-cities-and-districts-for-django/
+Your database must support spatial queries, see [GeoDjango]
+(https://docs.djangoproject.com/en/dev/ref/contrib/gis/) for setup.
+
+For more information see [this blog post]
+(http://www.coderholic.com/django-cities-countries-regions-cities-and-districts-for-django/).
Examples
-=======================
+--------------------------
Finding all London boroughs:
->>> london = City.objects.filter(country__name='United Kingdom').get(name='London')
->>> boroughs = District.objects.filter(city=london)
+```python
+ london = City.objects.filter(country__name='United Kingdom').get(name='London')
+ boroughs = District.objects.filter(city=london)
+```
Nearest city to a given geo-coord (longitude, latitude):
->>> City.objects.distance(Point(1, 51)).order_by('distance')[0]
-<City: Dymchurch, Kent, United Kingdom>
+```python
+ City.objects.distance(Point(1, 51)).order_by('distance')[0]
+ <City: Dymchurch, Kent, United Kingdom>
+```
5 Nearest cities to London:
->>> london = City.objects.filter(country__name='United Kingdom').get(name='London')
->>> nearest = City.objects.distance(london.location).exclude(id=london.id).order_by('distance')[:5]
+```python
+ london = City.objects.filter(country__name='United Kingdom').get(name='London')
+ nearest = City.objects.distance(london.location).exclude(id=london.id).order_by('distance')[:5]
+```
Get all countries in Japanese preferring official names if available, fallback on ASCII names:
->>> [country.alt_names_ja.get_preferred(default=country.name) for country in Country.objects.all()]
+```python
+ [country.alt_names_ja.get_preferred(default=country.name) for country in Country.objects.all()]
+```
Use alternate names model to get Vancouver in Japanese:
->>> geo_alt_names[City]['ja'].objects.get_preferred(geo__name='Vancouver', default='Vancouver')
+```python
+ geo_alt_names[City]['ja'].objects.get_preferred(geo__name='Vancouver', default='Vancouver')
+```
Gather names of Tokyo from all CITIES_LOCALES:
->>> [name for locale in cities.conf.settings.locales
+```python
+ [name for locale in cities.conf.settings.locales
for name in geo_alt_names[City][locale].objects.filter(geo__name='Tokyo')]
-
+```
Get all postal codes for Ontario, Canada (only first 3 letters available due to copyright restrictions):
->>> postal_codes['CA'].objects.filter(region_0_name='Ontario')
+```python
+ postal_codes['CA'].objects.filter(region_0_name='Ontario')
+```
Get region objects for US postal code:
->>> Region.objects.filter(postal_codes_US__code='90210')
-[<Region: Los Angeles County, California, United States>]
+```python
+ Region.objects.filter(postal_codes_US__code='90210')
+ [<Region: Los Angeles County, California, United States>]
+```
Get a list of cities in the state of Texas:
->>> City.objects.filter(country__name="United States", region__region_parent__name="Texas")
-
+```python
+ City.objects.filter(country__name="United States", region__region_parent__name="Texas")
+```
Install
-=======================
-- Run: python setup.py install
-- Add/Merge the following into your settings.py:
+--------------------------
+- Run: `python setup.py install`
+- Add/Merge the following into your *settings.py*:
-----------------------------------------------------------
+```python
INSTALLED_APPS = (
'cities',
)
@@ -106,14 +125,15 @@ CITIES_POSTAL_CODES = ['US','CA']
CITIES_PLUGINS = [
'cities.plugin.postal_code_ca.Plugin', # Canada postal codes need region codes remapped to match geonames
]
+```
-----------------------------------------------------------
-- Sync your database with the new models: 'manage.py syncdb'
-- Populate or update your database: 'manage.py cities'
+- Sync your database with the new models: `manage.py syncdb`
+- Populate or update your database: `manage.py cities`
Notes
-=======================
+--------------------------
The localized names and postal code models/db-tables are created dynamically based on your settings.
Some datasets are very large (> 100 MB) and take time to download / import, and there's no progress display.
Data will only be downloaded / imported if it is newer than your data, and only matching rows will be overwritten.
View
11 cities/management/commands/cities.py
@@ -433,12 +433,6 @@ def import_postal_code(self):
self.build_country_index()
self.build_region_index()
-
- self.logger.info("Building postal code index")
- postal_code_index = defaultdict(dict)
- for country in postal_codes:
- for obj in postal_codes[country].objects.all():
- postal_code_index[country][obj.code] = obj
self.logger.info("Importing postal codes")
for line in data:
@@ -459,11 +453,6 @@ def import_postal_code(self):
pc_type = postal_codes[country_code]
pc = pc_type()
-
- # Replace matching postal code in db if any
- try: pc.id = postal_code_index[country_code][code].id
- except: pass
-
pc.country = country
pc.code = code
pc.name = items[2]
View
2 cities/models.py
@@ -188,7 +188,7 @@ def name_full(self):
fields = {
'country': models.ForeignKey(Country,
related_name = 'postal_codes_' + country),
- 'code': models.CharField(max_length=20, db_index=True),
+ 'code': models.CharField(max_length=20, primary_key=True),
'name': models.CharField(max_length=200, db_index=True),
'region_0_name': models.CharField(max_length=100, db_index=True, verbose_name="region 0 name (state)"),
'region_1_name': models.CharField(max_length=100, db_index=True, verbose_name="region 1 name (county)"),
View
4 setup.py
@@ -10,15 +10,15 @@ def read(fname):
setup(
name='django-cities',
- version='0.181',
+ version='0.19',
description='Place models and data for Django apps',
author='Dan Carter (original by Ben Dowling)',
author_email='carterd@gmail.com',
url='https://github.com/Kometes/django-cities',
packages=find_packages(),
include_package_data=True,
zip_safe=False,
- long_description=read('README'),
+ long_description=read('README.md'),
license = "MIT",
keywords = "django cities countries regions postal codes geonames",
classifiers = [

0 comments on commit e102b56

Please sign in to comment.
Something went wrong with that request. Please try again.