Skip to content

Commit

Permalink
Update DRF field_mapping during initialization
Browse files Browse the repository at this point in the history
Makes GeoModelSerializer obsolete.
  • Loading branch information
nemesifier committed Jul 17, 2015
1 parent 3a8834e commit fb6ed36
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 28 deletions.
26 changes: 23 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ Install development version
pip install https://github.com/djangonauts/django-rest-framework-gis/tarball/master
Setup
-----

Add ``rest_framework_gis`` in ``settings.INSTALLED_APPS``, after ``rest_framework``:

.. code-block:: python
INSTALLED_APPS = [
# ...
'rest_framework',
'rest_framework_gis',
# ...
]
Compatibility with DRF, Django and Python
-----------------------------------------

Expand Down Expand Up @@ -53,6 +67,9 @@ Provides a ``GeometryField``, which is a subclass of Django Rest Framework
geometry fields, providing custom ``to_native`` and ``from_native``
methods for GeoJSON input/output.

**New in 0.9.3:** there is no need to define this field explicitly in your serializer,
it's mapped automatically during initialization in ``rest_framework_gis.apps.AppConfig.ready()``.

GeometrySerializerMethodField
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand All @@ -63,8 +80,11 @@ method and are used as a ``geo_field``. `See example below <https://github.com/d
Serializers
-----------

GeoModelSerializer
~~~~~~~~~~~~~~~~~~
GeoModelSerializer (DEPRECATED)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

**Deprecated, will be removed in 1.0**: Using this serializer is not needed anymore since 0.9.3 if you add
``rest_framework_gis`` in ``settings.INSTALLED_APPS``

Provides a ``GeoModelSerializer``, which is a sublass of DRF
``ModelSerializer``. This serializer updates the field\_mapping
Expand Down Expand Up @@ -114,7 +134,7 @@ In contrast, the ``GeoModelSerializer`` will output:
GeoFeatureModelSerializer
~~~~~~~~~~~~~~~~~~~~~~~~~

``GeoFeatureModelSerializer`` is a subclass of ``GeoModelSerializer``
``GeoFeatureModelSerializer`` is a subclass of ``rest_framework.ModelSerializer``
which will output data in a format that is **GeoJSON** compatible. Using
the above example, the ``GeoFeatureModelSerializer`` will output:

Expand Down
14 changes: 14 additions & 0 deletions rest_framework_gis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,17 @@ def get_version():
if VERSION[3] != 'final':
version = '%s %s' % (version, VERSION[3])
return version


default_app_config = 'rest_framework_gis.apps.AppConfig'

# retain support for django 1.5 and 1.6
try:
import django
import os

if os.environ.get('DJANGO_SETTINGS_MODULE') and django.get_version() < '1.7':
from .apps import AppConfig
AppConfig().ready()
except ImportError:
pass
36 changes: 36 additions & 0 deletions rest_framework_gis/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
try:
from django.apps import AppConfig as BaseConfig
except ImportError: # django <= 1.6
class BaseConfig(object):
pass


class AppConfig(BaseConfig):
name = 'rest_framework_gis'

def ready(self):
"""
update Django Rest Framework serializer mappings
"""
from django.contrib.gis.db import models
from rest_framework.serializers import ModelSerializer
from .fields import GeometryField

try:
# drf 3.0
field_mapping = ModelSerializer._field_mapping.mapping
except AttributeError:
# drf 3.1
field_mapping = ModelSerializer.serializer_field_mapping

# map GeoDjango fields to drf-gis GeometryField
field_mapping.update({
models.GeometryField: GeometryField,
models.PointField: GeometryField,
models.LineStringField: GeometryField,
models.PolygonField: GeometryField,
models.MultiPointField: GeometryField,
models.MultiLineStringField: GeometryField,
models.MultiPolygonField: GeometryField,
models.GeometryCollectionField: GeometryField
})
32 changes: 15 additions & 17 deletions rest_framework_gis/serializers.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
from django.core.exceptions import ImproperlyConfigured
from django.contrib.gis.db.models.fields import GeometryField as django_GeometryField
from django.contrib.gis.geos import Polygon

from rest_framework.serializers import ModelSerializer, ListSerializer, LIST_SERIALIZER_KWARGS
from rest_framework.utils.field_mapping import ClassLookupDict

from .fields import GeometryField, GeometrySerializerMethodField # noqa
from .utils import OrderedDict

# map drf-gis GeometryField to GeoDjango Geometry Field
try:
_geo_field_mapping = ModelSerializer._field_mapping.mapping
except AttributeError:
_geo_field_mapping = ModelSerializer.serializer_field_mapping
_geo_field_mapping.update({
django_GeometryField: GeometryField
})


class GeoModelSerializer(ModelSerializer):
"""
A subclass of DFR ModelSerializer that adds support
for GeoDjango fields to be serialized as GeoJSON
compatible data
Deprecated, will be removed in django-rest-framework-gis 1.0
"""
_field_mapping = ClassLookupDict(_geo_field_mapping)
def __init__(self, *args, **kwargs):
# TODO: remove in 1.0
from .apps import AppConfig
import warnings
import rest_framework_gis
AppConfig('rest_framework_gis', rest_framework_gis).ready()
warnings.simplefilter('always', DeprecationWarning)
warnings.warn('\nGeoModelSerializer is deprecated, '
'add "rest_framework_gis" to settings.INSTALLED_APPS and use '
'"rest_framework.ModelSerializer" instead',
DeprecationWarning)
super(GeoModelSerializer, self).__init__(*args, **kwargs)


class GeoFeatureModelListSerializer(ListSerializer):
Expand All @@ -42,9 +40,9 @@ def to_representation(self, data):
))


class GeoFeatureModelSerializer(GeoModelSerializer):
class GeoFeatureModelSerializer(ModelSerializer):
"""
A subclass of GeoModelSerializer
A subclass of ModelSerializer
that outputs geojson-ready data as
features and feature collections
"""
Expand Down
4 changes: 2 additions & 2 deletions rest_framework_gis/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
try:
from collections import OrderedDict
from collections import OrderedDict # noqa
# python 2.6
except ImportError: # pragma: no cover
from ordereddict import OrderedDict
from ordereddict import OrderedDict # noqa
6 changes: 1 addition & 5 deletions tests/django_restframework_gis_tests/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,19 @@
]


class LocationGeoSerializer(gis_serializers.GeoModelSerializer):
class LocationGeoSerializer(serializers.ModelSerializer):
""" location geo serializer """
details = serializers.HyperlinkedIdentityField(view_name='api_location_details')

class Meta:
model = Location
geo_field = 'geometry'


class PaginatedLocationGeoSerializer(pagination.PageNumberPagination):
page_size_query_param = 'limit'
page_size = 40
max_page_size = 10000

class Meta:
object_serializer_class = LocationGeoSerializer


class LocationGeoFeatureSerializer(gis_serializers.GeoFeatureModelSerializer):
""" location geo serializer """
Expand Down
1 change: 0 additions & 1 deletion tests/django_restframework_gis_tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from .serializers import *



class LocationList(generics.ListCreateAPIView):
model = Location
serializer_class = LocationGeoSerializer
Expand Down

0 comments on commit fb6ed36

Please sign in to comment.