Admin - delete with removing FK objects

9 views
Skip to first unread message

galgal

unread,
Sep 13, 2011, 7:52:25 PM9/13/11
to django...@googlegroups.com
I have 2 models

class Article(models.Model):
    active = models.BooleanField(default=False, db_index=True)
    title = models.CharField(max_length=150)
class ArticleGallery(models.Model):
    article = models.ForeignKey(Article)
    image = models.ImageField(upload_to=settings.ARTICLE_GALLERY_IMG_UPLOAD_TO)
    
    def delete(self, *args, **kwargs):
        self.image.delete()
        super(ArticleGallery, self).delete(*args, **kwargs)

But when i click delete in Article and submit it, elements are removed from database but ArticleGallery.delete() isn't fired up.
I need the delete method to be run when deleting Article. Any clues?

Jani Tiainen

unread,
Sep 14, 2011, 1:32:45 AM9/14/11
to django...@googlegroups.com

This is "known" behavior of Django admin. For efficiency reasons Django
uses queryset delete method which does not call delete() method.

You can though write your own delete hook in admin to traverse through
all instances.

But be aware that approach you're using has it's caveats: what if
deletion stops at some point? You have deleted images from the disk but
database _will_ be rolled back thus leaving you with records without images.

--

Jani Tiainen

galgal

unread,
Sep 14, 2011, 4:57:46 PM9/14/11
to django...@googlegroups.com
I made what I wanted using signals:)
All works now. I use post delete to be sure that object is deleted, than I remove images.
Reply all
Reply to author
Forward
0 new messages