Skip to main content

Django package for location services

Project description

CI Status PyPI Version Python Versions Django Versions License

Django package for location services.

  • Free software: BSD-3-Clause

Features

  • 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

Requirements

  • Python 3.9+

  • Django 4.2+

  • PostgreSQL with PostGIS extension

  • Django REST Framework

  • django-rest-framework-gis

Architecture

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 Location model that inherits from AbstractBaseLocation. Use this for quick setup when you don’t need customization.

Installation Options

Option 1: Using Abstract Models

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"

Option 2: Using Concrete Models (Quick Start)

Use the provided concrete Location model:

INSTALLED_APPS = [
    ...,
    "rest_framework",
    "rest_framework_gis",
    "location_api_core",  # Core models
    "location_api",       # Concrete Location model
]

Quick Start

  1. 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
  2. Add the required packages to your INSTALLED_APPS (see Installation Options above).

  3. 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")),
  4. (Optional) If using abstract models, configure your custom model:

    LOCATION_API_LOCATION_MODEL = "your_app.Location"
  5. Run migrations to create the models:

    python manage.py migrate
  6. Start the development server and visit the admin site to create locations.

  7. Access the API endpoints:

    • /api/location/ - List and create locations

    • /api/location/{id}/ - Retrieve, update, delete specific location

    • /api/location/search/ - Search locations

API Usage Examples

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

Models

AbstractBaseLocation

Base abstract model with common location fields:

  • name - Location name

  • point - PostGIS Point field for coordinates

  • country_code - ISO country code

  • created/modified - Timestamps

  • attributes - JSON field for additional data

  • archived - Soft delete flag

  • supplier - 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 matching

  • supplier - Foreign key to Supplier

Development

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

Credits

This package was created with Cookiecutter and the briggySmalls/cookiecutter-pypackage project template.

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_location_api-0.3.0.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

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

django_location_api-0.3.0-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file django_location_api-0.3.0.tar.gz.

File metadata

  • Download URL: django_location_api-0.3.0.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.4 {"installer":{"name":"uv","version":"0.11.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for django_location_api-0.3.0.tar.gz
Algorithm Hash digest
SHA256 bedf3826b9cb512c301c0382f0254e614d1003fb258d3d2a4086387415ddc424
MD5 9d29a936ef11dee786d7edc61dd026f0
BLAKE2b-256 5330f13cba723e8ed6c1fdd745c7fbf461c20271b84bf7a1b98e3811b7ac39ea

See more details on using hashes here.

File details

Details for the file django_location_api-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: django_location_api-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.4 {"installer":{"name":"uv","version":"0.11.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for django_location_api-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8986c382c4394703e31b2d128c99ae095b11606405fef2f05828733fa0099cec
MD5 6828e637d47615c8427e52a64e5e0372
BLAKE2b-256 e780f92350152809fb75e7a7a062d268a21c2d81a227ac4030e810eaaba4cfd1

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