Upload multiple images in Django

I want to upload multiple images but it can’t upload multiple images at the same time.
Please help me fix them
I have two models, one with Chapter and ChapterImage. I want to include all fields in the serializer and the related field
models.py

from django.db import models
import uuid
from manga.models import *
# Create your models here.
import os
class Chapter(models.Model):
    # ... other fields ...
    images = models.ManyToManyField('ChapterImage')
class ChapterImage(models.Model):
    image = models.ImageField(upload_to='chapter_images/')
    def __str__(self):
        return str(self.page)
    def get_file_path(instance, filename):
    ext = filename.split('.')[-1]
    filename = "%s.%s" % (uuid.uuid4(), ext)
    return os.path.join('chapters', filename)
class Chapter(models.Model):
    _id = models.UUIDField(default=uuid.uuid4,  unique=True,
                           primary_key=True, editable=False)
    manga = models.ForeignKey(Manga, on_delete=models.SET_NULL, null=True)
    number = models.IntegerField(null=True, blank=True)
    name = models.CharField(max_length=200, null=True, blank=True)
    views = models.IntegerField(null=True, blank=True, default=0)
    createdAt = models.DateTimeField(auto_now_add=True)

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



class ChapterImage(models.Model):
    _id = models.UUIDField(default=uuid.uuid4,  unique=True,
                           primary_key=True, editable=False)
    chapter = models.ForeignKey(Chapter, on_delete=models.SET_NULL, null=True)
    image = models.FileField(
        null=True, blank=True, default='/chapters/default.png', upload_to=get_file_path)
    page = models.IntegerField(null=True, blank=True)
    createdAt = models.DateTimeField(auto_now_add=True)

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

serializers.py

from rest_framework import serializers

from .models import Chapter, ChapterImage


class ChapterImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ChapterImage
        fields = ['image']

class ChapterSerializer(serializers.ModelSerializer):
    images = ChapterImageSerializer(many=True)

    class Meta:
        model = Chapter
        fields = ['title', 'images']

class ChapterSerializer(serializers.ModelSerializer):
    class Meta:
        model = Chapter
        fields = '__all__'


class ChapterDetailSerializer(serializers.ModelSerializer):
    chapterImages = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Chapter
        fields = '__all__'

    def get_chapterImages(self, obj):
        images = obj.chapterimage_set.all()
        serializer = ChapterImageSerializer(images, many=True)
        return serializer.data

Views.py

from django.shortcuts import render

from rest_framework.decorators import api_view, permission_classes
from rest_framework.response import Response
from rest_framework.views import APIView
from chapter.models import Chapter
from chapter.serializers import ChapterDetailSerializer, ChapterSerializer

from rest_framework import status

# Create your views here.

@api_view(['GET'])
def getChapter(request, pk):
    try:
        chapter = Chapter.objects.get(_id=pk)
        chapter.views += 1
        chapter.save()

        serializer = ChapterDetailSerializer(chapter, many=False)
        return Response(serializer.data)
    except Exception as e:
        return Response({'details': f"{e}"}, status=status.HTTP_204_NO_CONTENT)


@api_view(['GET'])
def getChaptersByManga(request, pk):
    try:
        chapters = Chapter.objects.filter(manga__pk=pk)
        serializer = ChapterSerializer(chapters, many=True)
        return Response(serializer.data)
    except Exception as e:
        return Response({'details': f"{e}"}, status=status.HTTP_204_NO_CONTENT)
    
class ChapterCreateView(APIView):
    def post(self, request):
        serializer = ChapterSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response({'message': 'Chapter created successfully'})
        return Response(serializer.errors, status=400)

Welcome @ChiiChi92 !

Side note: Thank you for posting your code as a code block. If you notice, I edited your post to show each file as a separate code block. It’s generally helpful to do that to make it more obvious when you’re posting code from multiple files.

You’ve posted two different definitions for the class ChapterSerializer, where the second version is going to override the first. The ChapterSerializer being used is the version without the images field.

Thanks you very much
Since I am newbie I don’t fully understand the problem
Coud you please tell me ,how do i solve it

You should only have one class definition for ChapterSerializer. Remove the one that isn’t supposed to be in there.

sorry,i try changed but it didn’t work.
it can only single upload :frowning: