当我运行保存覆盖方法时,mp3文件将保存到指定的文件夹中,只有它不断将自身保存到该目录中,直到最终重启服务器为止。
文件保存在正确的位置,可以用VLC播放,所以至少有...
看来,我包含的两个保存模型仍然存在问题。 我猜测super()。save()从未运行过,但我不能真正说出发生了什么,简单地说。
我在做错什么,为什么会发生多次储蓄?我该如何解决?
from django.db import models
from PIL import Image
from gtts import gTTS
from io import BytesIO
import tempfile
from django.core.files import File
class VoiceModel(models.Model):
name = models.CharField(max_length=50)
...
audiofile = models.FileField(upload_to='sounds/loads', max_length=100, blank=True, null=True) # editable=False)
def __str__(self):
return self.name
def save(self, *args, **kwargs):
new_string = 'repeat after me: ' + str(self.name)
file_name = '{}.mp3'.format(str(self.name).lower().replace(' ', '_'))
make_sound = gTTS(text=new_string, lang='en')
mp3_fp = BytesIO()
make_sound.write_to_fp(mp3_fp)
self.audiofile.save(file_name, mp3_fp)
super(VoiceModel, self).save(*args, **kwargs)
# def save(self, *args, **kwargs):
# new_string = 'repeat after me: ' + str(self.name)
# audiofile = gTTS(text=new_string, lang='en')
# with tempfile.TemporaryFile(mode='wb+') as f:
# # with tempfile.TemporaryFile(mode='w') as f:
# # with tempfile.TemporaryFile(mode='rb+') as f:
# audiofile.write_to_fp(f)
# file_name = '{}.mp3'.format(self.name).lower().replace(' ', '_')
# self.audiofile.save(file_name, File(file=f))
# super(VoiceModel, self).save(*args, **kwargs)
答案 0 :(得分:0)
在save方法中添加save=False
可以解决问题。
self.audiofile.save(file_name, mp3_fp, save=False)