[Fixed]-Use additional request param to generate a model field in DRF 3

1👍

How do you think serializer will know about purpose of latitute and longitute fields?
You should override create method and set you current_location manually

class InterfaceSerializer(serializers.ModelSerializer):
    latitude = serializers.FloatField()
    longitude = serializers.FloatField()

    class Meta:
        model = Interface
        fields = ('id', 'name', 'latitude', 'longitude',)

    def create(self, validated_data):
        latitute = validated_data.get('latitude')
        longitude = validated_data.get('longitude')
        name = validated_data.get('name')
        # suppose you want to store it charfield comma separated
        current_location = str(latitute) + ',' + str(longtitute)
        return Interface.objects.create(
                         current_location=current_location,
                         name=name
                         )

There is also useful package django-geoposition it provides field and widget for geoposition.

Leave a comment