Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

serialization - unable to upload multi files through django serializer

I can upload single files at a time but cant upload multiple files at once. Any idea how can I upload multifiles ?

my models.py

class Posts(models.Model):
   
    title = models.CharField("Title", max_length=500, blank=True, null=True)
    text = models.TextField("Text", blank=True, null=True)
    url = models.URLField("URL", blank=True, null=True)
    video_url = models.CharField("Video Url", max_length=500, blank=True, null=True)
    user = models.ForeignKey("profiles.HNUsers", on_delete=models.DO_NOTHING)


    class Meta:
        verbose_name_plural = "Posts"


class VideoPost(models.Model):
    user = models.ForeignKey("profiles.HNUsers", on_delete=models.DO_NOTHING)
    video = models.FileField("Post Video", blank=True, null=True)
    timestamp = models.DateTimeField("Timestamp", blank=True, null=True, auto_now_add=True)
    text = models.TextField("Description text", blank=True)

    class Meta:
        verbose_name_plural = "VIdeo Posts"

my serializers.py

class VideoPostSerializer(serializers.ModelSerializer):

    class Meta:
        model = VideoPost
        fields = (
            'user',
            'text',
            'video',
        )

and my views.py

postman enter image description here

Extended question : updated my VideoPostSerializer

class VideoPostSerializier(serializers.ModelSerializer):
    video = VideoPostSerializer(many=True, required=False)
    class Meta:
        model = VideoPost
        fields = (
            'user',
            'text',
            'video',
        )

    def create(self, validated_data):
        videos = validated_data.pop('video')
        post = VideoPost.objects.create(**validated_data)
        if videos:
            videos_instance = [VideoPost(post=post, video=video) for video in videos]
            VideoPost.objects.bulk_create(videos_instance)
        return post

Now I am facing this error in my POSTMAN enter image description here

Postman new error enter image description here

question from:https://stackoverflow.com/questions/65914002/unable-to-upload-multi-files-through-django-serializer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The point is that if you want the main model to have a lot of images or videos. You cannot add it as a field in the main model. You need to create a new model that will be associated with the main

for example i can show you my code with multi FileField upload

my models

class VideoPost(models.Model):
    user = models.ForeignKey("profiles.HNUsers", on_delete=models.DO_NOTHING)
    timestamp = models.DateTimeField("Timestamp", blank=True, null=True, auto_now_add=True)
    text = models.TextField("Description text", blank=True))


class PostVideo(models.Model):
    video_post = models.ForeignKey('VideoPost', models.CASCADE, related_name='post_video')
    video = models.FileField('Image', upload_to='post/images/'

my serializers:

class PostVideoSerializer(serializer.ModelSerializer):
    class Meta:
        model = PostVideo
        fields = '__all__'


class VideoPostSerialzier(serialzer.ModelSerializer):
    post_video = PostVideoSerializer(many=True, required=False)
    class Meta:
        model = VideoPost
        fields = '__all__'

    def create(self, validated_data):
        videos = validated_data.pop('post_video')
        post = VideoPost.objects.create(**validated_data)
        if videos:
            videos_instance = [PostVideo(post=post, video=video) for video in videos]
            PostVideo.objects.bulk_create(images_instance)
        return post

now you just need to call serializer.save() in your view

but you need a specific request like:

{
    'text': 'sometext',
    'user': 20
    'post_video': [IMAGEDATA1, IMAGEDATA2]
}

Also I wrote it blindly so there may be some mistakes but i hope you will have it as an example to rewrite your code

UPDATED - PostsSerialzier and request example

postman body example enter image description here

UPDATED 2 - updated models and serailzers


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...