I am creating a Django based web application, where Post Model will have multiple images and videos. I want to upload those multiple images in images model and videos in video model
# models.py
#Post Model
class Post(models.Model):
# Owner / user
user = models.ForeignKey(to=User, on_delete=models.CASCADE)
caption = models.CharField(max_length=64, verbose_name='Post Caption')
body = models.CharField(max_length=1024, verbose_name='Post Body')
def __str__(self):
return self.user.email
def get_author(self):
return self.user.email
class Image(models.Model):
post = models.OneToOneField(to=Post, on_delete=models.CASCADE)
image = models.ImageField()
class Video(models.Model):
post = models.OneToOneField(to=Post, on_delete=models.CASCADE)
image = models.FileField()
# serializers.py
class PropertySerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Property
fields = '__all__'
def create(self, validated_data):
media_files = self.context.get('view').request.FILES
post = Property.objects.create(
**validated_data
)
for file in media_files.values():
"""
if file is video:
then file will be saved in video model
if file is image:
then file will be saved in image model
How to do it?
"""
And how to validate if files are only image or video. If someone post any other things except image or video then it will give validation error. Is there any good way to it?