我想扩展Django模型,但不想让我的models.py
太胖。我决定将业务逻辑放在分离的类和文件中,并在models.py
中继承它。
我的情况下输入错误。
appname/models.py
:
from .models_bl.content import ContentBL
...
def upload_to_video(instance, filename):
ext = os.path.splitext(filename)[1]
name = uuid.uuid4().hex[:12]
basename = name + ext
return os.path.join('video', strftime("%Y-%m", gmtime()), basename)
...
@python_2_unicode_compatible
class Content(ContentBL):
...
video = models.FileField(upload_to=upload_to_video, blank=True)
video_thumb = models.ImageField(upload_to=upload_to_video, blank=True)
...
appname/models_bl/content.py
:
# not works (import error)
from appname.models import upload_to_video
...
class ContentBL(models.Model):
def save(self, *args, **args):
...
if video_changed:
# works, but bad practice, I guess
from appname.models import upload_to_video
# need upload_to_video() from models.py HERE
video_thumb_filename = upload_to_video('image.jpg')
command = "ffmpeg -i %s -vframes 1 -f image2 %s"
os.system(command % (self.video.path, video_thumb_filename))
self.video_thumb = video_thumb_filename
...
使它运作的最佳方法是什么?