Home > other >  Django not able to update field with serialzier
Django not able to update field with serialzier

Time:10-12

Hi I've my serializer like this

class PredefinedHabitSerializer(serializers.ModelSerializer):

    image= serializers.SerializerMethodField()

    class Meta:
        model = models.PredefinedHabit
        fields = [
            "id",
            "habit",
            "category",
            "why",
            "how",
            "dos",
            "donts",
            "info",
            "alternative",
            "image"
        ]

    def get_image(self, obj):
        if  obj.image.startswith("https"):
            return obj.image
        else:
            return aws_get(obj,"s3-debug")

My serializer get_image method is used to get the presigned URL of the image key stored in the model field image

And a view to edit this model

def edit_predefined_habit(request):
        predefined_habit = PredefinedHabit.objects.get(id=request.data.get("id"))
        data = {
            "category": request.data.get("category"),
            "habit": request.data.get("habit"),
            "why": request.data.get("why"),
            "how": request.data.get("how"),
            "dos": request.data.get("dos"),
            "donts": request.data.get("donts"),
            "info": request.data.get("info"),
            "alternative": request.data.get("alternative"),
            "image": request.data.get("illustration"),
        }
        data = Functions.clean(data)
        serializer = PredefinedHabitSerializer(predefined_habit, data=data)
        if serializer.is_valid():
            serializer.save()
            return Response(data={"status": "success"})

View to get all objects

def viewPredefinedHabits(request):
        model = PredefinedHabit.objects.all()
        serializer = PredefinedHabitSerializer(model, many=True)
        return Response(serializer.data)

I am not able to update my image field with edit API, with my understanding it is because I've specified image to be a serializer method field. How do I overcome this? I've tried write_only doesn't work

Other way I can think of manually saving into object and .save without passing to serializer.

CodePudding user response:

You can build serializers for each request type.

class PredefinedHabitReadSerializer(serializers.ModelSerializer):

    image= serializers.SerializerMethodField()

    class Meta:
        model = models.PredefinedHabit
        fields = [
            "id",
            "habit",
            "category",
            "why",
            "how",
            "dos",
            "donts",
            "info",
            "alternative",
            "image"
        ]

    def get_image(self, obj):
        if  obj.image.startswith("https"):
            return obj.image
        else:
            return aws_get(obj,"s3-debug")

class PredefinedHabitUpdateSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.PredefinedHabit
        fields = [
            "habit",
            "category",
            "why",
            "how",
            "dos",
            "donts",
            "info",
            "alternative",
            "image"
        ]
    def to_representation(self, instance):
        return PredefinedHabitReadSerializer(instance).data

After you can use PredefinedHabitUpdateSerializer when you need to update your model instance. If you need to retrieve or list your query, you can use PredefinedHabitReadSerializer.

  • Related