我有以下模型,其中包含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