0

I tried many times, but I just register the last one

im my model

class PropertyImage(models.Model):
    property = models.ForeignKey(Property, default=None, on_delete=models.CASCADE,)
    images = models.ImageField(upload_to=upload, null=True, blank=True)

    def __str__(self):
        return str(self.images)

serializer

class PropertyImageSerializers (serializers.ModelSerializer):
    class Meta:
        model = PropertyImage
        #fields =('name','')
        fields = '__all__'

my class view handler the post request, I tried to user method FOR to loop all images and save

view

        def post(self, request, *args, **kwargs):
        property_id = request.data['property']
        form_data = {}

        for images in request.FILES.getlist('images'):

            form_data['property']= property_id
            form_data['images']=images

            print(form_data)

            serializer = PropertyImageSerializers(data=form_data)

            if serializer.is_valid():
                serializer.save()
                return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

the for not give me the loop, does't matter how many images I send

1 Answer 1

0

I get this error message: AttributeError: 'PropertyImageSerializers' object has no attribute 'property' but im my model you can see I have the property

property is not an attribute of the PropertyImageSerializers class, that's why AttributeError

I think you will find your answer here

Update:

You can do it like this

def post(self, request, *args, **kwargs):
    property_id = request.data['property']
    form_data = {}
    form_data['property']= property_id
    success = True
    response = []
    for images in request.FILES.getlist('images'):
        form_data['images']=images
        print(form_data)
        serializer = PropertyImageSerializers(data=form_data)
        if serializer.is_valid():
            serializer.save()
            response.append(serializer.data)
        else:
            success = False
    if success:
        return Response(response, status=status.HTTP_201_CREATED)
    return Response(response,status=status.HTTP_400_BAD_REQUEST)
Sign up to request clarification or add additional context in comments.

3 Comments

I change I little, the for not works I can send many images but I not get loop
@JúniorOaks the way you did it, the post function may return a response just after the first iteration, which is not the desired behavior, right? you should return a response after successfully processing all images.
thanks @md nasfis khan, you right, and I did not append value to the list with the new image, thanks, is running a 100%

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.