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
UPDATED 2 - updated models and serailzers
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…