0

I am designing the endpoint that receive single POST with multiple images. Here is my simple endpoint by Django REST

models.py

class CaseImage(models.Model):
    image = models.ImageField(upload_to='case_images')
    issue = models.ForeignKey(CaseIssue, related_name='images', related_query_name='images', on_delete=models.CASCADE)

serializers.py

class MyImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = CaseImage
        fields = [
            'id',
            'image',
        ]


class NewCaseIssueSerializer(serializers.ModelSerializer):
    images = MyImageSerializer(many=True)

    class Meta:
        model = CaseIssue
        fields = [
            'id',
            'machine',
            'symptom',
            'images',
            'activities',
        ]

    def create(self, validated_data):
        from pprint import pprint
        import ipdb; ipdb.set_trace()
        images = validated_data.pop('images')
        instance = super().create(validated_data)
        tmp = []
        for image in images:
            tmp.append(CaseImage(issue=instance, image=image))
        CaseImage.objects.bulk_create(tmp)

        return instance

I am imitating this answer enter image description here

My break point found empty list of images

> /Users/sarit/Code/mp_resource/mp_resource/case_issues/api/serializers.py(115)create()
    114         import ipdb; ipdb.set_trace()
--> 115         images = validated_data.pop('images')
    116         instance = super().create(validated_data)

ipdb> validated_data
{'machine': <Machine: eNYfySyXcDgplaeDXKbxxwmEYbFawyOOgDOhKaUTtBNExPxVbC>, 'symptom': 'First symptom', 'images': []}

Questions:
1. Is it practical to POST multiple images in single shot?
2. If it is practical. How to do that in Postman application?

joe
  • 8,383
  • 13
  • 61
  • 109

1 Answers1

0

Handle your data before send with javascript... add all your images in one single body and send it to your endpoint... in your backend receive your payload and get each image and do whatever you want

Diego Vinícius
  • 2,125
  • 1
  • 12
  • 23