I am able to upload a single image with the following code. If I select multiple images then only the last image among the selected image is getting uploaded.
models.py
class Image(models.Model):
property_id = models.ForeignKey(
'properties.Address',
null=False,
default=1,
on_delete=models.CASCADE
)
image = models.ImageField(upload_to=directory_path)
serializers.py
class ImageSerializer(serializers.ModelSerializer):
class Meta:
model = Image
fields = (
'property_id',
'image'
)
views.py
class ImageView(APIView):
parser_classes = (MultiPartParser, FormParser)
def get(self, request):
all_images = Image.objects.all()
serializer = ImageSerializer(all_images, many=True)
return JsonResponse(serializer.data, safe=False)
def post(self, request, *args, **kwargs):
file_serializer = ImageSerializer(data=request.data)
if file_serializer.is_valid():
file_serializer.save()
return Response(file_serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
I am a little new to Django. I want to loop over my array of images which is received in the request.data Can anyone tell me how to do it?