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

How Deserialize data from geojson format #253

Open irfanpule opened 3 years ago

irfanpule commented 3 years ago

example I have serializer

class PointSerializer(GeoFeatureModelSerializer):
    class Meta:
        model = PointModel
        fields = '__all__'
        geo_field = 'geom'

and I get serialize data or response

{
    "type": "FeatureCollection",
    "features": [
        {
            "id": 395,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    106.92374152131379,
                    -6.938941860571504
                ]
            },
            "properties": {
                "date": "2020-02-10",
                "time": "17:15:04",
                "video_sec": 7,
                "speed": 3.406,
                "road_number": 1
            }
        },
        {
            "id": 432,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    106.92334589548409,
                    -6.940676830708981
                ]
            },
            "properties": {
                "date": "2020-02-10",
                "time": "17:15:41",
                "video_sec": 44,
                "speed": 6.093,
                "photo_file": "",
                "road_number": 1
            }
        }
    ]
}

How I deserializer this response ?

devkapilbansal commented 3 years ago

@irfanpule may be you want to convert string you get back to dictionary If that's the case, you can use json.loads(response.data) to deserialize it

lukemckinstry commented 1 year ago

it will deserialize a feature, not a FeatureCollection

{
            "id": 395,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    106.92374152131379,
                    -6.938941860571504
                ]
            },
            "properties": {
                "date": "2020-02-10",
                "time": "17:15:04",
                "video_sec": 7,
                "speed": 3.406,
                "road_number": 1
            }
        }

so you can pass the features array from your request body to your PointSerializer model like so:

PointSerializer(data=request.data["features"], many=True)`