Upload many images django rest framework
Solution 1:
I have write the code for you. Try this:
class PostCreateSerializer(ModelSerializer):
title = serializers.CharField()
content = serializers.CharField()
price = serializers.CharField()
duration = serializers.CharField()
destination = serializers.CharField()
images = serializers.ListField(child=serializers.ImageField())
_id = serializers.CharField()
class Meta:
model = Post
fields = [
'title',
'content',
'price',
'duration',
'destination',
'images'
]
def create(self,validated_data):
title = validated_data['title']
content = validated_data['content']
price = validated_data['price']
duration = validated_data['duration']
destination = validated_data['destination']
images = validated_data['images']
_id = validated_data['_id']
post_obj = Post.objects.create(
title=title,
content = content,
price=price,
duration=duration,
destination=destination,
)
_id = Post.objects.get(id=_id)
if not _id.exists():
raise ValidationError('id does not exists')
PostImage.objects.bulk_create([PostImage(post=_id,image=i) for i in images])
return validated_data