master branch of the Django REST framework.GeoFeatureModelSerializerclass Location(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='locations')
city = models.CharField(max_length=128)
state = models.CharField(max_length=64, null=True)
country_code = models.CharField(max_length=2)
country = models.CharField(max_length=64)
point = models.PointField(null=True)
class LocationSerializer(GeoFeatureModelSerializer):
class Meta:
model = Location
geo_field = "point"
fields = ('city', 'state', 'country', 'point', 'tag')
GeoFeatureModelSerializer is from: https://github.com/openwisp/django-rest-framework-gis
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-123.0208,
44.0464
]
},
"properties": {
"city": "string",
"state": "string",
"country": "string",
}
}
{
"city": "string",
"state": "string",
"country": "string",
"point": "string",
}
Here, the serializer is generating some custom output using serializer fields. openapi generates schema directly from fields present in the serializer which will not work in these cases.
What I want from DRF: Provide a guideline/approach to override schema generation. I am open to raising a Pull request.
Once we have a solution, I will raise an issue in django-rest-framework-gis. This library will provide a custom schema.
django-rest-framework-gis should create an AutoSchema subclass that you would use to map GeoFeatureModelSerializer subclasses.
The simplest would look for a GeoFeatureModelSerializer in _map_serializer() and handle it, or hand off to super().
We'll promote _map_serializer() a public map_serializer() now I think.
I'll leave this open to track that for now.
Many thanks for the prompt reply. In #6418, you proposed the same idea.
Rather than overriding entire AutoSchema, I think mixin with required overridden methods would be a better choice.
As of now, _map_serializer() is a private method. This should be overridden by any library because DRF can remove this method in any future release without any notice. It would be great if you can provide a solution which is future proof.
We'll promote _map_serializer() a public map_serializer() now I think.
Override _map_serializer() for now. We will make it public, as I said.
AutoSchema is not a big class. Mixins are overkill for this.
I've drafted the docs for this. I'm going to close it to keep my TODO list functioning.
Most helpful comment
I've drafted the docs for this. I'm going to close it to keep my TODO list functioning.