sorryMike
sorryMike

Reputation: 789

Add non-model field in Serializer DRF

I'm trying to combine rasters model field with non-model config_version field into my serializer which looks like:

class RasterPostSerializer(serializers.ModelSerializer):

    class Meta:
        model = Raster
        fields = (
            'name',
            'description',
            'filepath'
        )


class ConfigurationSerializer(serializers.Serializer):
    config_version = serializers.CharField()
    rasters = RasterPostSerializer(many=True)

    def create(self, validated_data):
        data_field = validated_data['rasters']
        for raster in data_field['rasters']:
            return Raster(name=raster['name'], description=raster['description'], filepath=raster['filepath'])

Before utilize serializer.save() method I would like to check config_version in my view.py, but after that the .save() gives me:

The serializer field might be named incorrectly and not match any attribute or key on the `Raster` instance.
Original exception text was: 'Raster' object has no attribute 'config_version'.

What is going on, and is there a solution for that?

EDIT: my example JSON POST payload looks like:

{
  "config_version":"v2",
  "rasters":[
    {
      "name":"XYZ.jpg",
      "description":"some description",
      "filepath":"path in filesystem"
    }
}

The config_version it is not constant. It can be set dynamically, due the controller utilizing ConfigurationSerializer will be the configuration endpoint - there need to be possibility to add rasters, and some other information in the future.

configuration endpoint in views.py looks like

@api_view(['POST'])
def configuration(request):

if request.method == 'POST':
    serializer = ConfigurationSerializer(data=request.data)

    if serializer.is_valid():
        if serializer.validated_data['config_version'] is 'v2':

            raster_name_list = [raster.name for raster in Raster.objects.all()]
            for instance in serializer.validated_data['rasters']:
                if instance['name'] in raster_name_list:
                    return Response({"Fail": "Raster name '{}' already exists".format(instance['name'])}, status=status.HTTP_304_NOT_MODIFIED)
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

Upvotes: 4

Views: 3551

Answers (2)

Aneesh R S
Aneesh R S

Reputation: 3827

You should return ConfigurationSerializer data as the result of create action. And also i have no idea what you are going to do with config_version

class ConfigurationSerializer(serializers.Serializer):
    config_version = serializers.CharField()
    rasters = RasterPostSerializer(many=True)

    def create(self, validated_data):
        config_version = validated_data.get('rasters')
        rasters = validated_data.pop('rasters')
        raster_list = []
        for raster in rasters:
            raster_list.append(Raster.objects.create(**raster))
        return {
            "config_version": config_version,
            "rasters": raster_list
        }

Upvotes: 2

wm3ndez
wm3ndez

Reputation: 723

You need a SerializerMethodField: http://www.django-rest-framework.org/api-guide/fields/#serializermethodfield

config_version = SerializerMethodField()

def get_config_version(self, obj):
    return YOUR_CUSTOM_DATA

Upvotes: 0

Related Questions