0

I have a simple form in my app which contains manytomany field and an document uploader.

The data in this form is being sent as multipart/form-data

for e.g.

transaction_no: 5001
document: (binary)
items: [{"product":1,"quantity":"12","fault":"Repairable"}]
allotment: 2

The create function works perfectly fine but when I try to edit the data in this form I get an error The data sent during "editing" is similar to the data sent during "creation".

data:

transaction_no: 5001
items: [{"id":5,"quantity":"10","fault":"Repairable","product":1}]
allotment: 2

Serializer.py

class DeliveredItemsSerializer(serializers.ModelSerializer):

    class Meta:
        model = DeliveredItems
        fields = "__all__"

class DeliveredSerializer(serializers.ModelSerializer):

    items = DeliveredItemsSerializer(many=True, required=False)

    class Meta:
        model = Delivered
        fields = "__all__"


    def create(self, validated_data):
        items_objects = validated_data.pop('items', None)
        prdcts = []
        try:
            for item in items_objects:
                i = DeliveredItems.objects.create(**item)
                prdcts.append(i)
            instance = Delivered.objects.create(**validated_data)
            instance.items.set(prdcts)
            return instance
        except:
            instance = Delivered.objects.create(**validated_data)
            return instance

    def update(self, instance, validated_data):
        items_objects = validated_data.pop('items',None)
        prdcts = []
        for item in items_objects:
            print("item", item)
            fk_instance, created = DeliveredItems.objects.update_or_create(pk=item.get('id'), defaults=item)
            prdcts.append(fk_instance.pk)
        instance.items.set(prdcts)
        instance = super(DeliveredSerializer, self).update(instance, validated_data)
        return instance

Error:

'NoneType' object is not iterable

and this is the line of error:

for item in items_objects:

How come when I am sending the same data in exact same format is not received by the serializer during update?

Rahul Sharma
  • 2,187
  • 6
  • 31
  • 76
  • check `self.initial_data` inside the `update()` method and ensure that the `items` field is there with *data* – JPG Oct 13 '20 at 09:43
  • @ArakkalAbu `self.initial_data` return this data ``.. So the data is being received, right ? – Rahul Sharma Oct 13 '20 at 09:48
  • seems like the entire `items` is a `string` – JPG Oct 13 '20 at 09:49
  • @ArakkalAbu I just checked the `initial_data` during creation and it's `{'transaction_no': '5000', 'items': [{'product': 1, 'quantity': '12', 'fault': 'Repairable'}], 'allotment': '1', 'owner': 2, 'delivered': True}` .. Yeah! you are right the `items` is in string as well as other changes such as `transaction_no` is in a list.. How does this happen ? – Rahul Sharma Oct 13 '20 at 09:55
  • I suspect that the client is not using the same *content type* on both time – JPG Oct 13 '20 at 09:57
  • @ArakkalAbu I solved this by doing some preprocessing.. Can you tell me how can I upload multiple files with this ? – Rahul Sharma Oct 13 '20 at 11:12
  • probably this, https://stackoverflow.com/questions/48756249/ – JPG Oct 13 '20 at 11:20

0 Answers0