E
E
Egegey2021-07-29 18:26:09
Django
Egegey, 2021-07-29 18:26:09

Loading and saving multiple images in M2M field Django Rest?

No matter how I tried, I could not fill the dictionary with validated_dataa list of images that is sent to the server.
The key imagesis not in the dictionary, the method validate_imagesdoes not run. C cover_imageeverything is all right Tell me
, is it possible to fill the dictionary validated_datawith a validated list of images (InMemoryUploadFile), other than how to get raw files from request.FILES?

6102c8b8c891b604061551.png
Am I sending via PostMan correctly?

class Image(models.Model):
    name = models.CharField(max_length=255, blank=True)
    image = models.ImageField(upload_to='images/', validators=[validation_existing_image])

class Address(models.Model):
    cover_image = models.ForeignKey(Image, on_delete=models.SET_NULL, null=True, related_name='+', blank=True)
    images = models.ManyToManyField(Image, related_name='+', blank=True)

class UpdateAddressSerializer(serializers.ModelSerializer):
    cover_image = serializers.ImageField()
    images = serializers.ListSerializer(child=serializers.ImageField())

    class Meta:
        model = Address
        fields = '__all__'

        def validate_images(self, value):
            return value

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egegey, 2021-08-05
@Egegey

The jamb in this line:

images = serializers.ListSerializer(child=serializers.ImageField())

ListSerializer needs to be replaced with ListField and everything worked.
Although everything is correctly indicated in the dock, I just did not notice

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question