0

I am sending axios.patch request with form-data in vue

axios.patch(`${API_BASE}/products/${id}/`, data, {
  headers: { 'Content-Type': 'multipart/form-data'
}

and calling Django ModelViewset partial update

    class MyViewSet(viewsets.ModelViewSet):
        def update(self, request, *args, **kwargs):
            data = request.data.copy()
            question = self.get_object()
            ...

the problem is that I am getting all values in stringified form.. null values as 'null', integer values as '1', and so on. enter image description here

What should I do to get normal values(null as None, integer as int) in request.data?

  • Use serializer and set `partial=True` in serializer initialisation. Eg: `serializer = YourSerializer(request, data=request.data, partial=True)` – zaidfazil Mar 05 '18 at 07:02
  • @zaidfazil Thanks for reply, but the question is about "I receive null values as 'null' type of string in request.data. What should I do in order to receive null values as None in request.data? – Zhussipnazar Daulet Mar 05 '18 at 07:05
  • `request.data` is always a `QueryDict`,. Serializers are used for interpreting the data from the request. Write a serializer for your data and the validated_data from the serializer would be of what you can use. – zaidfazil Mar 05 '18 at 08:47
  • @zaidfazil got you, thanks! – Zhussipnazar Daulet Mar 06 '18 at 03:24

2 Answers2

0

What should I do to get normal values(null as None, integer as int) in request.data?

Either use JSON or use the serializer's validated data. HTML forms are sent as strings in the forms of key/value pairs.

Linovia
  • 19,812
  • 4
  • 47
  • 48
  • Hi, thanks for your reply. I can't use JSON, because my form contains images and I can't get validated data, because I am overriding ModelViewset's update method and getting the request.data in order to do some manipulation. – Zhussipnazar Daulet Mar 05 '18 at 07:10
  • So it's down to you doing the cast yourself. – Linovia Mar 05 '18 at 07:50
0

solved this by setting null values as empty strings before the patch method and by setting allow_null=True in serializers