【发布时间】:2011-08-17 21:49:03
【问题描述】:
我想在我的模型中覆盖 save 方法以创建适当的 slug 并创建图像域的副本,并在其中进行少量修改。我该如何处理?
def save(self, *args, **kwargs):
super(MyModel, self).save(*args, **kwargs) #to get id
#slug
self.slug = '%s-%i' % (self.topic, self.id)
#create copy of img
cp_path = dirname(self.image.path)+'/copies_'+basename(self.image.path)
shutil.copy2(self.image.path, cp_path)
file = open(cp_path)
django_file = File(file)
django_file.name = basename(cp_path) #otherwise path will be duplicated
self.cp_image = django_file
super(MyModel, self).save(*args, **kwargs) #to save my new ImageField
create_watermark(self.cp_image, self.topic, self.text, 500, 45)
因为我使用了 super(MyModel, self).save() 两次,所以我有一个 self.image 文件的副本。如您所见,我对 django 和 python 不是很熟悉。我怎样才能做得更好?
【问题讨论】: