Django REST Framework GIS

1.2.0 · active · verified Wed Apr 15

Django REST Framework GIS (DRF-GIS) provides geographic add-ons for Django Rest Framework, enabling the creation of REST APIs for geospatial data. It offers GIS-specific fields and serializers, including GeoJSON support. The current version is 1.2.0. Releases are somewhat irregular, with major updates typically annually, and minor versions released as needed.

Warnings

Install

Imports

Quickstart

This quickstart illustrates a basic Django model with a GIS `PointField`, a `GeoFeatureModelSerializer` to output GeoJSON, and a DRF `ListCreateAPIView`. It shows the essential components needed to expose geospatial data via a REST API. Note that this requires a properly configured Django project with `django.contrib.gis` and a spatial database (e.g., PostGIS) backend. The Django setup within the snippet makes it runnable as a standalone script for component definition check.

import os
import django
from django.conf import settings
from django.db import models
from django.contrib.gis.db import models as gis_models
from rest_framework_gis.serializers import GeoFeatureModelSerializer
from rest_framework.generics import ListCreateAPIView

# --- Minimal Django setup for demonstration ---
# In a real Django project, these would be in settings.py, models.py, views.py, urls.py

if not settings.configured:
    settings.configure(
        INSTALLED_APPS=[
            'django.contrib.admin',
            'django.contrib.auth',
            'django.contrib.contenttypes',
            'django.contrib.sessions',
            'django.contrib.messages',
            'django.contrib.staticfiles',
            'django.contrib.gis', # Essential for DRF-GIS
            'rest_framework',
            'rest_framework_gis',
            'myapp' # Placeholder for our app, add it to INSTALLED_APPS
        ],
        DATABASES={
            'default': {
                'ENGINE': 'django.contrib.gis.db.backends.postgis',
                'NAME': 'mydatabase_test',
                'USER': os.environ.get('DB_USER', 'postgres'),
                'PASSWORD': os.environ.get('DB_PASSWORD', ''),
                'HOST': os.environ.get('DB_HOST', 'localhost'),
                'PORT': os.environ.get('DB_PORT', '5432'),
            }
        },
        SECRET_KEY=os.environ.get('DJANGO_SECRET_KEY', 'a-very-secret-key-for-dev-and-testing'),
        ROOT_URLCONF='__main__',
        USE_TZ=True,
        TIME_ZONE='UTC',
        STATIC_URL='/static/'
    )
    django.setup()

# --- Example Model (myapp/models.py) ---
# Define a custom AppConfig for 'myapp' if needed, or simply ensure 'myapp' is in INSTALLED_APPS
# and Django can find your models.
class Place(gis_models.Model):
    name = models.CharField(max_length=100)
    location = gis_models.PointField(srid=4326) # WGS84 latitude/longitude

    def __str__(self):
        return self.name
    
    class Meta:
        app_label = 'myapp' # Required for models defined outside an app directory

# --- Example Serializer (myapp/serializers.py) ---
class PlaceSerializer(GeoFeatureModelSerializer):
    class Meta:
        model = Place
        geo_field = 'location'
        fields = ('id', 'name', 'location')

# --- Example View (myapp/views.py) ---
class PlaceListCreateAPIView(ListCreateAPIView):
    queryset = Place.objects.all()
    serializer_class = PlaceSerializer

# --- Example URLs (project_root/urls.py or myapp/urls.py) ---
# from django.urls import path
# from myapp.views import PlaceListCreateAPIView
#
# urlpatterns = [
#     path('api/places/', PlaceListCreateAPIView.as_view(), name='place-list-create'),
# ]

# This block checks if the components are defined. In a real project,
# you would configure URLs and run the Django development server.
assert issubclass(PlaceSerializer, GeoFeatureModelSerializer)
assert issubclass(PlaceListCreateAPIView, ListCreateAPIView)
print("DRF-GIS model, serializer, and view components defined successfully.")

view raw JSON →