简体   繁体   中英

django-rest-framework-gis related field

I have a geographical model structure where multiple events can have the same location:

class Event(models.Model):
    name = models.CharField(max_length=128, blank=True, null=True)
    location = models.ForeignKey('MarketLocation', null=True, blank=True)

class EventLocation(models.Model):
    location = models.PointField(srid=4326)

I am using the GeoFeatureModelSerializer provided by django-rest-framework-gis to output a single JSON object but the PointField is being rendered as a string instead of a coordinate pair:

So it is giving me:

"location": "POINT (-1.909 53.7094)"

Instead of:

  "point": {
        "type": "Point",
        "coordinates": [-123.0208, 44.0464],
   },

The logical answer would be to define the field in the serializer:

geo_field = eventlocation__location

But that doesn't appear to make any difference to the output, which makes me think that it probably doesn't work but it probably should. Has anybody made this work and if so how?

I found this this morning, and it also applies to DRF-gis:

Django Rest Framework - Get related model field in serializer

I created a serializer on EventLocation and defined it as 'location' in EventSerializer and the point is output as a geojson geometry.

I'm doing something similar, but with MultiPolygon instead of Point. Here's my serializer:

class AreaSerializer(gis_serializers.GeoFeatureModelSerializer):

    class Meta:
        model = Area
        geo_field = "geom"

Perhaps there isn't the need to reference the model in geo_field, instead just stating the field directly?

Here's the repo for my Serializer, if that might help:

https://github.com/kelvinn/alerted-us-web/blob/master/apps/alertdb/serializers.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM