我有以下模型,其中包含FileField
,其中用户提供包含图片的zip文件。在保存期间,该zip文件通过名为process_zipfile()
的方法进行处理。Django:保存期间填充字段()
class Album(models.Model):
nom = models.CharField(max_length = 200)
added_by = models.ForeignKey(User, null=True, blank=True)
gallery = models.ForeignKey(Gallery, null=True, blank=True)
zip_file = models.FileField('image field .zip', upload_to=PHOTOLOGUE_DIR+"/temp",
help_text='Select a .zip file of images to upload into a new Gallery.')
class Meta:
ordering = ['nom']
def save(self, *args, **kwargs):
self.gallery = self.process_zipfile()
super(Album, self).save(*args, **kwargs)
def delete(self, *args, **kwargs):
photos = self.gallery.photos.all()
for photo in photos:
photo.delete()
self.gallery.delete()
super(Album, self).delete(*args, **kwargs)
def process_zipfile(self):
if os.path.isfile(self.zip_file.path):
......(creates gallery object and links the photos)
return gallery
它工作的很好,除了现场gallery
(由左表格空白)不是由process_zipfile()
创建画廊填充。我做错了什么?
此外,删除方法似乎没有工作,任何想法?
process_zipfile()的返回类型是什么?你确定它是Gallery模型类型吗? – 2011-03-15 16:40:41
@kesun:是使用'gallery = Gallery.objects.create(title = self.nom,title_slug = slugify(self.nom))' – Mermoz 2011-03-15 17:10:20
关于删除方法:有时(特别是当你通过django-admin删除时)方法永远不会被调用。更好地使用pre_delete/post_delete信号来确保您想要在删除中执行的操作始终完成。 – 2011-03-16 10:06:05