# Upload multiple images in Django

**URL:** <https://forum.djangoproject.com/t/upload-multiple-images-in-django/35644>\
**Category:** Forms & APIs\
**Created:** [October 13, 2024, 9:28am UTC](https://forum.djangoproject.com/t/upload-multiple-images-in-django/35644 "2024-10-13T09:28:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChiiChi92](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/chiichi92/32/24579_2.png) [@ChiiChi92](https://forum.djangoproject.com/u/ChiiChi92)\
**Post date:** [October 13, 2024, 9:28am UTC](https://forum.djangoproject.com/t/upload-multiple-images-in-django/35644/1 "2024-10-13T09:28:45Z")

</div>

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

```auto
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

```auto
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

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 13, 2024, 12:24pm UTC](https://forum.djangoproject.com/t/upload-multiple-images-in-django/35644/2 "2024-10-13T12:24:55Z")

</div>

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.

> [@ChiiChi92](#):
>
> ```auto
> 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__'
> 
> ```

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.

---

<div class="post-metadata">

**Author:** ![ChiiChi92](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/chiichi92/32/24579_2.png) [@ChiiChi92](https://forum.djangoproject.com/u/ChiiChi92)\
**Post date:** [October 13, 2024, 12:35pm UTC](https://forum.djangoproject.com/t/upload-multiple-images-in-django/35644/3 "2024-10-13T12:35:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 13, 2024, 12:36pm UTC](https://forum.djangoproject.com/t/upload-multiple-images-in-django/35644/4 "2024-10-13T12:36:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ChiiChi92](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/chiichi92/32/24579_2.png) [@ChiiChi92](https://forum.djangoproject.com/u/ChiiChi92)\
**Post date:** [October 14, 2024, 3:52pm UTC](https://forum.djangoproject.com/t/upload-multiple-images-in-django/35644/5 "2024-10-14T15:52:31Z")

</div>

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