I've created an endpoint that will return some serialized models (available and unavailable) and a couple of geographic points (sw and ne) that define the bounding box around those models. I'll determine the extents of all of the Listings returned and will defined sw and ne accordingly.
I'd like to do something like this:
class ListingSerializer(serializers.ModelSerializer):
photos = ListingPhotoSerializer(many=True, read_only=True)
class Meta:
model = Listing
exclude = []
class ListingAvailabilityBBoxSerializer(serializers.Serializer):
available = ListingSerializer(many=True, read_only=True)
unavailable = ListingSerializer(many=True, read_only=True)
sw = PointSerializer(read_only=True)
ne = PointSerializer(read_only=True)
But PointSerializer for Point (as defined in django.contrib.gis.geos) doesn't exist, as far as I know. Is there a PointSerializer I haven't yet found? How can I do this?
I came up with something that works best for me. At first I tried installing the
drf-extra-fieldspackage and creating a serializer like so:That worked as far as I tested, but it didn't play well with
drf-spectacular, which I'm using for documentation. Plus it serializes points slightly differently that themodel.PointFieldsthat were already in my existing models.The best solution for me was to create a
Modeljust for the purposes of creating a model-based serializer. I setmanagedtoFalseso it wouldn't create a table in the database.