问题
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?
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/28142981/django-rest-framework-gis-related-field