Skip to main content

A Django package for geographical data (regions, subregions, countries, states, cities)

Project description

Geodata: A Django Package for Geographical Data

Overview

Geodata is a Django package that provides models for regions, subregions, countries, states, and cities, populated with data from the countries-states-cities-database. It simplifies the integration of geographical data into Django projects, suitable for applications requiring location-based functionality.

Credit for the data goes to countries-states-cities-database

Installation

Install the package using pip:

pip install geodata

Setup

Add 'geodata' to your INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    # ...
    'geodata',
    # ...
]

Run migrations to create the database tables:

python manage.py migrate

Loading Data

Populate the database with geographical data using the management command:

python manage.py load_geodata

This command loads regions, subregions, countries, states, and cities from JSON files included in the package.

Performance Options

For faster loading during development, you can skip loading cities (which is the largest dataset):

python manage.py load_geodata --skip-cities

You can also adjust the batch size for bulk operations:

python manage.py load_geodata --batch-size 2000

Models

The package provides the following models:

  • Region: Represents a geographical region (e.g., Africa).
  • SubRegion: Represents a subregion within a region (e.g., Western Africa).
  • Country: Represents a country with details like ISO codes, capital, currency, etc.
  • State: Represents a state or province within a country.
  • City: Represents a city within a state and country, including latitude and longitude.

Each model includes fields as defined in the data source, with foreign key relationships to maintain the hierarchy. The models also include database indexes on commonly queried fields for improved performance.

Key Features

  • Translations: Region, SubRegion, and Country models include support for translations in multiple languages
  • Coordinates: State and City models include a convenient coordinates property to access latitude and longitude as a tuple
  • Timezones: Country model includes timezone data as a JSONField with a helper method get_timezones()
  • Database Indexes: All models include appropriate indexes for optimized queries

Usage Examples

Querying Cities in a State

To retrieve all cities in a specific state:

from geodata.models import State, City

state = State.objects.get(name='California')
cities = City.objects.filter(state=state)
for city in cities:
    print(city.name)

Creating a Form with Country and City Fields

Use the models in a Django form:

from django import forms
from geodata.models import Country, City

class LocationForm(forms.Form):
    country = forms.ModelChoiceField(queryset=Country.objects.all())
    city = forms.ModelChoiceField(queryset=City.objects.all())

Filtering Cities by Country

To get cities in a specific country:

from geodata.models import Country, City

country = Country.objects.get(name='United States')
cities = City.objects.filter(country=country)

Working with Coordinates

from geodata.models import City

city = City.objects.get(name='New York')
lat, lng = city.coordinates  # Returns tuple of (latitude, longitude)

Accessing Translations

from geodata.models import Country

country = Country.objects.get(name='Germany')
german_name = country.translations.get('de')  # Returns 'Deutschland'

Data Source and License

The data is sourced from the countries-states-cities-database, licensed under the Open Database License. Users must comply with the license terms and independently verify the data for critical applications.

Updating Data

The data is updated periodically. To refresh the data:

  1. Download the latest JSON files from the repository.
  2. Replace the files in the geodata/data/ directory.
  3. Re-run the load_geodata command:
python manage.py load_geodata

Note: This will update existing data, so back up your database if necessary.

Performance Considerations

  • The complete dataset is large, especially the cities data. Consider using the --skip-cities flag during development.
  • Database indexes are provided on foreign keys and commonly queried fields to improve query performance.
  • For large-scale applications, consider implementing caching strategies for frequently accessed data.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This package is licensed under the MIT License.

Changelog

1.1.1 (2025-07-05)

  • Fixed data loading issues with optional fields:
    • Made native field in Country model optional
    • Made latitude and longitude fields in State model optional
    • Added proper handling of None values in coordinates
    • Added error handling for missing regions/subregions
  • Improved data loading robustness:
    • Added warnings for missing region/subregion references
    • Better handling of incomplete data
    • More graceful error recovery during data loading

1.0.0

  • Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_geodata_mjomba-1.1.1.tar.gz (11.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_geodata_mjomba-1.1.1-py3-none-any.whl (12.1 MB view details)

Uploaded Python 3

File details

Details for the file django_geodata_mjomba-1.1.1.tar.gz.

File metadata

  • Download URL: django_geodata_mjomba-1.1.1.tar.gz
  • Upload date:
  • Size: 11.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for django_geodata_mjomba-1.1.1.tar.gz
Algorithm Hash digest
SHA256 954716e4fccfb93d579cfc8f8ce108e64cec16c7b4831547b2c7abf6fba6216c
MD5 fec5b31b3eb72abffbde758d297d3f1b
BLAKE2b-256 39b6e8c2809579be903e74d3feff40189dc817bb557d673526cdc762966e1764

See more details on using hashes here.

File details

Details for the file django_geodata_mjomba-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_geodata_mjomba-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3e481cdbbdc8a51a4d89cde861a3c8cd12c52d46072e6812dd6428967feaabda
MD5 cd0f9bc99e963f9f7af37519bfe6f651
BLAKE2b-256 a2094f8a4596acdd4d16e02c8ec5cc5b4a7825b773d398c00e70657ba288a701

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page