我有一个包含3个文件字段的模型,并在调用.save时对它们进行操作。问题是保存任何FileField都会触发对象的.save。如何一次保存多个文件字段?
class Record(Model):
name = CharField(max_length=30)
audio = FileField(upload_to=settings.AUDIO_ROOT)
alt_audio = FileField(upload_to=settings.AUDIO_ROOT, null=True)
sample = FileField(upload_to=settings.AUDIO_ROOT, null=True)
def save(self, *args, **kwargs):
convert_files(self)
super(Record, self).save(*args, **kwargs)
当音频上传(mp3)时,它会从mp3转换为ogg(反之亦然),并保存到alt_audio和sample中:
def convert_files(record):
...
record.alt_audio.save(os.path.basename(convert_to), File(open(convert_to)))
record.sample.save(os.path.basename(sample_name), File(open(sample_name, r )))
问题是alt_audio.save
触发返回record.save
。如果每个文件字段不为空,我必须添加对它的检查。我还想推迟行动,稍后把它交给芹菜服务器。有没有办法不触发多次保存?