Get default value for serializer field from a method

Viewed 36

I struggle to find a way to do something I thought must be very basic. I have a serializer accepting either address (nested serializer) or coordinates (also a nested serializer). If the user supplies the address, I resolve it using some magic into coordinates and I want to provide those coordinates in the response. This is what I have so far:

class AddressSerializer(serializers.Serializer):
    zip_code = serializers.CharField()
    city = serializers.CharField(required=False)
    street = serializers.CharField(required=False)
    house_number = serializers.CharField(required=False)

class GeoPointSerializer(serializers.Serializer):
    latitude = serializers.FloatField()
    longitude = serializers.FloatField()


class TheSerializer(serializers.Serializer):
    address = AddressSerializer(required=False)
    coordinates = GeoPointSerializer(required=False)

    def validate(self, attrs):
        # check if the user provided either address or coordinates

    def resolve_address_to_coordinates(...):
        ...

and now I have no idea how to continue. I know about SerializerMethodField which would work fine, except it is read-only. Then I was playing with default and source attributes on the coordinates field, but also no success. Can anyone unstuck me?

0 Answers
Related