openwisp / django-rest-framework-gis

Geographic add-ons for Django REST Framework. Maintained by the OpenWISP Project.
http://openwisp.org
MIT License
1.09k stars 201 forks source link

Deprecation of GeoModelSerializer and selective opt in instead of default GeoFeatureModelSerializer? #207

Closed ludvikbrodl closed 4 years ago

ludvikbrodl commented 4 years ago

If GeoModelSerializer is removed and you are forced to place rest_framework_gis in settings.py. This would make all serializers in the project change behaviour to that of GeoModelSerializer, resulting in having to opt out of the behaviour of GEOJSON compatible format for all geometries.

Did I miss something or is there no way to use/opt in to the Geojson compatible serializer without settings rest_framework_gis in settings.py?

I found a quite cumbersome way to accomplish this but it feels hacky:

class BuildingSerializer(GeoFeatureModelSerializer):
    geom = GeometrySerializerMethodField()

    def get_geom(self, obj):
        return obj.geom

    class Meta:
        model = Building
        geo_field = "geom"
        fields = ("id", "name")

I would think that just doing:

class BuildingSerializer(GeoFeatureModelSerializer):
    class Meta:
        model = Building
        geo_field = "geom"
        fields = ("id", "name")

without rest_framework_gis in settings.py would be enough, but this does not output geojson compatible geometriy, but the usual WKT and gives of no warnings.

nemesifier commented 4 years ago

try:

from rest_framework_gis.fields import GeometryField

class BuildingSerializer(GeoFeatureModelSerializer):
    geom = GeometryField()

    class Meta:
        model = Building
        geo_field = "geom"
        fields = ("id", "name")
ludvikbrodl commented 4 years ago

That does work, although I still think that providing geo_field = "geom" should be enough

It is not trivial to understand this behaviour by just reading the README.

nemesifier commented 4 years ago

That's probably because the recommended way is to add rest_framework_gis to INSTALLED_APPS so that this code is executed: https://github.com/openwisp/django-rest-framework-gis/blob/master/rest_framework_gis/apps.py#L11-L32.

If rest_framework_gis is not added to INSTALLED_APPS the definition must be explicit.

Feel free to send a pull request to add further explanation if you think is useful for other people.