1

I have a post serializer and a postimage serializer to upload multiple images to a post. I have this serializer, but I am not sure how to make it in a way, so that I can upload multiple images, for example 5 images with a single post, like how we use with formsets coz now I can upload only 1 image in 'images' field.

These are the serializers. Please do have a look and let me know what changes I have to make...

Thanks

class PostImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = PostImage
        fields = ['id', 'images',]

class PostSerializer(TaggitSerializer, serializers.ModelSerializer):
    user = serializers.ReadOnlyField(source='user.username')
    post_date = serializers.ReadOnlyField()
    postimage_set = PostImageSerializer(many=True)
    likes = UserSerializer(many=True)
    class Meta:
        model = Post
        fields = ['id','title', 'post_date', 'updated', 'user', 'image', 'postimage_set']

    def create(self,validated_data):
        user = self.context['request'].user
        title = validated_data['title']
        image = self.context['request'].FILES.get('image')
        images = self.context['request'].FILES.get('images')

        m1 = Post(user=user,title=title,image=image,)
        m1.save()
        m2 = PostImage(post=m1, images= images)
        m2.save()
        validated_data['images']=m2.images
        validated_data['image']=m1.image
        return validated_data

views

class CreatePostAPIView(generics.CreateAPIView):
    serializer_class = PostCreateSerializer
    permission_classes = [IsAuthenticated]

    def create(self, request, *args, **kwargs):
        serializer = PostCreateSerializer(data=request.data, context={'request':request,})
        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)

3 Answers 3

2

This solved the problem

def create(self, validated_date):
    images = self.context['request'].FILES.getlist('images')

    for image in list(images):
        m2 = PostImage(post=m1, images= image)
        m2.save()
Sign up to request clarification or add additional context in comments.

Comments

0

This is how you do the multiple upload on drf: example below on create you can access the multiple item you posted:

e.g posted:

Formdata

images: File.jpeg
images: File2.jpeg
images: File3.jpeg

class TestSerializer(serializers.Serializer):
    images = serializers.ListField(child=serializers.FileField())


class CreatePostAPIView(generics.CreateAPIView):
    permission_classes = [AllowAny]
    serializer_class = TestSerializer

    def create(self, request, *args, **kwargs):
        images = request.data['images']

4 Comments

Hey thanks for the response, but I haven't really understood the above code and how to implement. I am just a beginner. :)
Try to follow the code above and print the images after def post, you will know what I meant.
Ok..I will try it out..Thanks
Hey I tried it out, its showing null and only 1 image is getting saved.
0

I would do it like this. Make a PostModel and a PostImageModel (with a post ForeignKey) for each post image. And then in the serializer, make relation field with both. Something like this:

models.py

class Post(Model):
    post details ...

class PostImage(Model):
    post = models.ForeigKey(Post, ...)
    image = models.ImageField(...)

serializers.py

class PostImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = PostImage
        fields = '__all__'

class PostSerializer(serialiers.ModelSerializer):
    
    images = serializers.SlugRelatedField(slug_field='image', many=True, ...)

    class Meta:
        model = Post
        fields = [..., 'images']

views.py

class CreatePostView(CreateView):
    classForm = PostForm (with images field)
    
    def form_valid(self, form):
        new_post = form.save(commit=False)
        for image in new_post.images:
            new_image = PostImage(post=new_post, image=image)
            new_image.save()
        new_post.save()
        return super()form_valid(form)

So then in your api you should be able to see the post model with the images attached to it.

1 Comment

Hey, thanks for the response. but does using a normal createview work with rest framework?

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.