If I have 1 model with 3 different geo_fields in (point, poly and line), can I serialize all of these with django-rest-framework-gis?
My model:
class Job(BaseModel):
name = models.CharField(max_length=64)
desc = models.CharField(max_length=64)
loc_poly = models.PolygonField(blank=True)
loc_polyline = models.LineStringField(blank=True)
loc_point = models.PointField(blank=True)
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
Can I serialize by doing something like:
class JobSerializer(GeoFeatureModelSerializer):
class Meta:
model = Job
geo_field = ("loc_point", "loc_polyline", "loc_poly")
fields = ('__all__',)
Basically can I have geo_field to be multiple geo fields? Or is this only 1?
No, it is not possible. Consider which of those three fields you want to represent your model if you were to display it on a map.
The
geo_field
inJobSerializer
must represent only one of your geo fields. This is because when it's de-serialized,geo_field
will be the representation of what gets displayed on a map.If you want all three of these features to be displayed simultaneously, I would consider creating intermediary models that link the job to it's feature. You could have something like
Job
that is just the name/desc and then 3 models (one for each GeoField type) that has the Job as a foreign key.If you want only one of those fields to display, you could setup the
geo_field
set to return based on some priority you have set and return the geo features separately like the example below: