Django package for location services.
- Free software: BSD-3-Clause
- Dual-app architecture: Choose between abstract base models or concrete implementations
- PostGIS integration: Full GIS support for location data
- Pluggable models: Use your own Location model while leveraging our views and serializers
- REST API: Complete API endpoints with Django REST Framework
- Admin integration: Ready-to-use Django admin interfaces
- Python 3.9+
- Django 4.2+
- PostgreSQL with PostGIS extension
- Django REST Framework
- django-rest-framework-gis
django-location-api provides two Django apps:
- location_api_core
- Contains abstract base models (
AbstractBaseLocation,Supplier,SupplierAlias) that you can inherit from in your own apps. Use this when you want to customize the Location model. - location_api
- Contains a concrete
Locationmodel that inherits fromAbstractBaseLocation. Use this for quick setup when you don't need customization.
Install only the core models and create your own Location model:
INSTALLED_APPS = [
...,
"rest_framework",
"rest_framework_gis",
"location_api_core", # Only install core
"your_app", # Your app with custom Location model
]
In your models.py:
from location_api_core.models import AbstractBaseLocation
class YourLocationModel(AbstractBaseLocation):
# Add your custom fields here
custom_field = models.CharField(max_length=100)
class Meta:
ordering = ("name",)
Configure the API to use your model in settings.py:
LOCATION_API_LOCATION_MODEL = "your_app.YourLocationModel"
Use the provided concrete Location model:
INSTALLED_APPS = [
...,
"rest_framework",
"rest_framework_gis",
"location_api_core", # Core models
"location_api", # Concrete Location model
]
Install using pip:
pip install django-location-api
Or using your preferred package manager:
# Using uv uv add django-location-api # Using poetry poetry add django-location-api
Add the required packages to your INSTALLED_APPS (see Installation Options above).
Include the location_api URL conf in your project urls.py:
path("api/", include("location_api.urls")), path("api/location/", include("location_api.urls.search")),(Optional) If using abstract models, configure your custom model:
LOCATION_API_LOCATION_MODEL = "your_app.Location"
Run migrations to create the models:
python manage.py migrate
Start the development server and visit the admin site to create locations.
Access the API endpoints:
/api/location/- List and create locations/api/location/{id}/- Retrieve, update, delete specific location/api/location/search/- Search locations
Creating a location via API:
POST /api/locations/
{
"name": "Central Park",
"point": {
"type": "Point",
"coordinates": [-73.965355, 40.782865]
},
"country_code": "US",
"supplier": 1,
"attributes": {
"type": "park",
"area": "843 acres"
}
}
Searching locations:
GET /locations/search/?term=park
Geographic queries (bounding box):
GET /api/locations/?in_bbox=-74.0,40.7,-73.9,40.8
- AbstractBaseLocation
Base abstract model with common location fields:
name- Location namepoint- PostGIS Point field for coordinatescountry_code- ISO country codecreated/modified- Timestampsattributes- JSON field for additional dataarchived- Soft delete flagsupplier- Foreign key to Supplier
- Supplier
Represents location suppliers/operators:
name- Supplier name- Built-in
identify()method for string matching
- SupplierAlias
Regex patterns for supplier identification:
regex- Pattern for matchingsupplier- Foreign key to Supplier
This project uses uv for dependency management and ships a Docker Compose setup that provides PostgreSQL + PostGIS and the full test environment.
Install dependencies locally (for formatting / linting only — the test suite requires PostGIS, which is easiest to get via Docker):
uv sync
Run the test suite (45 tests, all under Docker):
docker compose run --rm django python ./runtests.py
Run the example projects (see examples/ for walkthroughs):
# Basic example (concrete Location model) — http://localhost:8000 docker compose run --rm django python examples/basic/manage.py migrate docker compose up django # Custom example (AbstractBaseLocation + extra fields) — http://localhost:8001 docker compose run --rm -T custom-example python examples/custom/manage.py migrate docker compose --profile custom up custom-example
Format and lint:
uv run invoke format uv run invoke lint
Create / update migrations for the concrete location_api app:
docker compose run --rm django python examples/basic/manage.py makemigrations location_api
This package was created with Cookiecutter and the briggySmalls/cookiecutter-pypackage project template.