Django Signals - 如何保存实例
Django Signals - How to save the instance
我正在使用 Django Rest Framework
上传视频。现在我想为我的视频文件添加缩略图。每当保存视频对象时,我都想创建一个缩略图并设置其 thumbnail
字段。
我的视频模型如下所示:
class Video(models.Model):
created = models.DateTimeField(auto_now_add=True)
text = models.CharField(max_length=100, blank=True)
video = models.FileField(upload_to='Videos/',blank=True)
thumbnail = models.ImageField(upload_to = 'Images/', blank = True)
我的信号处理程序如下所示:
from moviepy.video.io.VideoFileClip import VideoFileClip
from posts.models import Video
from django.db.models.signals import post_save
from django.dispatch import receiver
from settingsFolderOfProject.settings import MEDIA_ROOT
import os
# when save() of Video is done, create_thumbnail_from_video() is called
@receiver(post_save, sender=Video)
def create_thumbnail_from_video(sender, instance, created, **kwargs):
if created:
# create the clip using moviepy's VideoFileClip class
clip = VideoFileClip(os.path.join(MEDIA_ROOT, instance.video.name))
# create the frame at 1st second and set it to instance's thumbnail field
instance.thumbnail = clip.save_frame(os.path.join(MEDIA_ROOT, 'Images/thumbnail.jpg'),t='00:00:01')
# save the instance
instance.save() # <--- I think this op does not work properly
我必须改变什么?
缩略图文件按预期在文件夹中创建,但 Django 没有设置我的 Video
模型实例的 thumbnail
字段。创建的 Video
实例的 thumbnail
字段仍设置为空。
clip.save_frame() 没有 return 任何东西。
相反做
path = os.path.join(MEDIA_ROOT, 'Images/thumbnail.jpg')
clip.save_frame(path,t='00:00:01')
instance.thumbnail = path
instance.save()
注:尚未测试。如果问题仍然存在,请发表评论。谢谢...
我正在使用 Django Rest Framework
上传视频。现在我想为我的视频文件添加缩略图。每当保存视频对象时,我都想创建一个缩略图并设置其 thumbnail
字段。
我的视频模型如下所示:
class Video(models.Model):
created = models.DateTimeField(auto_now_add=True)
text = models.CharField(max_length=100, blank=True)
video = models.FileField(upload_to='Videos/',blank=True)
thumbnail = models.ImageField(upload_to = 'Images/', blank = True)
我的信号处理程序如下所示:
from moviepy.video.io.VideoFileClip import VideoFileClip
from posts.models import Video
from django.db.models.signals import post_save
from django.dispatch import receiver
from settingsFolderOfProject.settings import MEDIA_ROOT
import os
# when save() of Video is done, create_thumbnail_from_video() is called
@receiver(post_save, sender=Video)
def create_thumbnail_from_video(sender, instance, created, **kwargs):
if created:
# create the clip using moviepy's VideoFileClip class
clip = VideoFileClip(os.path.join(MEDIA_ROOT, instance.video.name))
# create the frame at 1st second and set it to instance's thumbnail field
instance.thumbnail = clip.save_frame(os.path.join(MEDIA_ROOT, 'Images/thumbnail.jpg'),t='00:00:01')
# save the instance
instance.save() # <--- I think this op does not work properly
我必须改变什么?
缩略图文件按预期在文件夹中创建,但 Django 没有设置我的 Video
模型实例的 thumbnail
字段。创建的 Video
实例的 thumbnail
字段仍设置为空。
clip.save_frame() 没有 return 任何东西。
相反做
path = os.path.join(MEDIA_ROOT, 'Images/thumbnail.jpg')
clip.save_frame(path,t='00:00:01')
instance.thumbnail = path
instance.save()
注:尚未测试。如果问题仍然存在,请发表评论。谢谢...