{"id":6604,"library":"djangorestframework-gis","title":"Django REST Framework GIS","description":"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.","status":"active","version":"1.2.0","language":"en","source_language":"en","source_url":"https://github.com/openwisp/django-rest-framework-gis","tags":["django","drf","gis","geospatial","rest-api","serializer","geojson"],"install":[{"cmd":"pip install djangorestframework-gis","lang":"bash","label":"Install core library"},{"cmd":"pip install psycopg2-binary","lang":"bash","label":"Install PostGIS adapter (recommended)"}],"dependencies":[{"reason":"Core Django framework, requires Django 4.2+, 5.0+, 5.1+, 5.2+.","package":"django","optional":false},{"reason":"Core DRF framework, requires DRF 3.12+ (3.16.0+ recommended).","package":"djangorestframework","optional":false},{"reason":"Used for advanced filtering features, requires django-filter >= 2.0.","package":"django-filter","optional":true},{"reason":"Python adapter for PostgreSQL with PostGIS extension. Highly recommended for spatial databases.","package":"psycopg2-binary","optional":true}],"imports":[{"symbol":"GeometryField","correct":"from rest_framework_gis.fields import GeometryField"},{"symbol":"GeoFeatureModelSerializer","correct":"from rest_framework_gis.serializers import GeoFeatureModelSerializer"},{"symbol":"DistanceToPointOrderingFilter","correct":"from rest_framework_gis.filters import DistanceToPointOrderingFilter"}],"quickstart":{"code":"import os\nimport django\nfrom django.conf import settings\nfrom django.db import models\nfrom django.contrib.gis.db import models as gis_models\nfrom rest_framework_gis.serializers import GeoFeatureModelSerializer\nfrom rest_framework.generics import ListCreateAPIView\n\n# --- Minimal Django setup for demonstration ---\n# In a real Django project, these would be in settings.py, models.py, views.py, urls.py\n\nif not settings.configured:\n    settings.configure(\n        INSTALLED_APPS=[\n            'django.contrib.admin',\n            'django.contrib.auth',\n            'django.contrib.contenttypes',\n            'django.contrib.sessions',\n            'django.contrib.messages',\n            'django.contrib.staticfiles',\n            'django.contrib.gis', # Essential for DRF-GIS\n            'rest_framework',\n            'rest_framework_gis',\n            'myapp' # Placeholder for our app, add it to INSTALLED_APPS\n        ],\n        DATABASES={\n            'default': {\n                'ENGINE': 'django.contrib.gis.db.backends.postgis',\n                'NAME': 'mydatabase_test',\n                'USER': os.environ.get('DB_USER', 'postgres'),\n                'PASSWORD': os.environ.get('DB_PASSWORD', ''),\n                'HOST': os.environ.get('DB_HOST', 'localhost'),\n                'PORT': os.environ.get('DB_PORT', '5432'),\n            }\n        },\n        SECRET_KEY=os.environ.get('DJANGO_SECRET_KEY', 'a-very-secret-key-for-dev-and-testing'),\n        ROOT_URLCONF='__main__',\n        USE_TZ=True,\n        TIME_ZONE='UTC',\n        STATIC_URL='/static/'\n    )\n    django.setup()\n\n# --- Example Model (myapp/models.py) ---\n# Define a custom AppConfig for 'myapp' if needed, or simply ensure 'myapp' is in INSTALLED_APPS\n# and Django can find your models.\nclass Place(gis_models.Model):\n    name = models.CharField(max_length=100)\n    location = gis_models.PointField(srid=4326) # WGS84 latitude/longitude\n\n    def __str__(self):\n        return self.name\n    \n    class Meta:\n        app_label = 'myapp' # Required for models defined outside an app directory\n\n# --- Example Serializer (myapp/serializers.py) ---\nclass PlaceSerializer(GeoFeatureModelSerializer):\n    class Meta:\n        model = Place\n        geo_field = 'location'\n        fields = ('id', 'name', 'location')\n\n# --- Example View (myapp/views.py) ---\nclass PlaceListCreateAPIView(ListCreateAPIView):\n    queryset = Place.objects.all()\n    serializer_class = PlaceSerializer\n\n# --- Example URLs (project_root/urls.py or myapp/urls.py) ---\n# from django.urls import path\n# from myapp.views import PlaceListCreateAPIView\n#\n# urlpatterns = [\n#     path('api/places/', PlaceListCreateAPIView.as_view(), name='place-list-create'),\n# ]\n\n# This block checks if the components are defined. In a real project,\n# you would configure URLs and run the Django development server.\nassert issubclass(PlaceSerializer, GeoFeatureModelSerializer)\nassert issubclass(PlaceListCreateAPIView, ListCreateAPIView)\nprint(\"DRF-GIS model, serializer, and view components defined successfully.\")\n","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade Python to 3.8+ (3.13+ recommended), Django to 4.2+ (5.2+ recommended), and Django REST Framework to 3.12+ (3.16.0+ recommended).","message":"Version 1.1.0 dropped support for Python versions 3.6, 3.7 and Django versions 2.2, 3.0, 3.1, 4.0. It also dropped support for `djangorestframework < 3.12`.","severity":"breaking","affected_versions":">=1.1.0"},{"fix":"Upgrade Python to 3.4+ (see main breaking change for recommended versions) and `django-filters` to 2.0+.","message":"Version 0.14.0 introduced a dependency on `django-filters >= 2.0`, which itself requires Python >= 3.4. Ensure your Python version meets this requirement if using filtering.","severity":"breaking","affected_versions":">=0.14.0"},{"fix":"Upgrade `djangorestframework-gis` to version 1.0.0 or higher to resolve the `default_app_config` deprecation warning.","message":"Prior to version 1.0.0, Django versions >= 3.2 would raise a `default_app_config` deprecation warning when using `djangorestframework-gis`.","severity":"gotcha","affected_versions":"<1.0.0"},{"fix":"If you relied on `GeometryField`'s `style` argument, upgrade to version 0.16.0 or higher to ensure custom styles are applied correctly without being overridden.","message":"Before version 0.16.0, passing additional arguments via `style` to `GeometryField` might have been unintentionally overridden. This was fixed to allow proper customization.","severity":"gotcha","affected_versions":"<0.16.0"},{"fix":"Upgrade `djangorestframework-gis` to version 0.16.0 or higher to correctly handle and represent empty geometries in your API responses.","message":"Prior to version 0.16.0, `djangorestframework-gis` had issues with the representation of empty geometries, potentially leading to errors or incorrect output.","severity":"gotcha","affected_versions":"<0.16.0"},{"fix":"Upgrade `djangorestframework-gis` to version 1.0.0 or higher to ensure correct deserialization of the `id_field`.","message":"The deserialization of the `id_field` was fixed in version 1.0.0. If you experienced issues with `id_field` during deserialization (e.g., when creating or updating objects), this might have been the cause.","severity":"gotcha","affected_versions":"<1.0.0"},{"fix":"Upgrade `djangorestframework-gis` to version 1.1.0 or higher if you need to serialize models that may or may not have a geometry field.","message":"For `GeoFeatureModelSerializer` to correctly handle models without a geometry field, you must be using version 1.1.0 or higher.","severity":"gotcha","affected_versions":"<1.1.0"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}