Re: Method call when deleting FK in admin
Reason is that admin uses QuerySet.delete() (https://docs.djangoproject.com/en/1.3/ref/models/querysets/#delete) Note that deleting files while deleting models is dangerous: what happens if deletion is aborted and rolled back for some reason? You have now deleted files but instances stayed in a database. And you have lost data - and created inconsistency on top of that all. On Sat, Jul 23, 2011 at 12:07 AM, galgalwrote: > > > My models:class Spectacle(models.Model): > > title = models.CharField(max_length=100) > slug = models.SlugField(max_length=100 unique=False) > description = models.TextField() > class SpectacleGallery(models.Model): > spectacle = models.ForeignKey(Spectacle) > image = models.ImageField(upload_to=upload_path_handler, max_length=255, > help_text=_(u'tylko pliki z rozszerzeniem .jpg')) > image_thumb = models.CharField(max_length=255, editable=False, blank=True, > null=True) > def delete(self, *args, **kwargs): > > image_thumb_abs = settings.MEDIA_ROOT + str(self.image_thumb) > > # try to delete thumb and image > > try: > > remove(image_thumb_abs) > > except Exception: > > pass > > try: > > self.image.delete() > > except Exception: > > pass > > super(SpectacleGallery, self).delete(*args, **kwargs) # Call the "real" > delete() method. > > #try to delete dir > > try: > > removedirs(settings.SPECTACLE_GALLERY_UPLOAD_PATH + str(self.spectacle_id)) > > except: > > pass > > > Now in admin, when I delete from SpectacleGallery file is being deleted > from disk and DB properly. The problem is when I want to delete Spectacle. > Deletion page is asking if i'm sure to delete Spectacle and all related > images. I confirm and: all records from database are deleted. Unfortunately > files from SpectacleGallery are not deleted.I paste print into delete() > method in SpectacleGallery. Nothing shows so my question is: Why delete > method is not called when deleting it in that way? > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/django-users/-/6o66VmCVQqMJ. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Method call when deleting FK in admin
try "admins.ModelAdmin" On Sat, Jul 23, 2011 at 5:07 AM, galgalwrote: > > > My models:class Spectacle(models.Model): > > title = models.CharField(max_length=100) > slug = models.SlugField(max_length=100 unique=False) > description = models.TextField() > class SpectacleGallery(models.Model): > spectacle = models.ForeignKey(Spectacle) > image = models.ImageField(upload_to=upload_path_handler, max_length=255, > help_text=_(u'tylko pliki z rozszerzeniem .jpg')) > image_thumb = models.CharField(max_length=255, editable=False, blank=True, > null=True) > def delete(self, *args, **kwargs): > > image_thumb_abs = settings.MEDIA_ROOT + str(self.image_thumb) > > # try to delete thumb and image > > try: > > remove(image_thumb_abs) > > except Exception: > > pass > > try: > > self.image.delete() > > except Exception: > > pass > > super(SpectacleGallery, self).delete(*args, **kwargs) # Call the "real" > delete() method. > > #try to delete dir > > try: > > removedirs(settings.SPECTACLE_GALLERY_UPLOAD_PATH + str(self.spectacle_id)) > > except: > > pass > > > Now in admin, when I delete from SpectacleGallery file is being deleted > from disk and DB properly. The problem is when I want to delete Spectacle. > Deletion page is asking if i'm sure to delete Spectacle and all related > images. I confirm and: all records from database are deleted. Unfortunately > files from SpectacleGallery are not deleted.I paste print into delete() > method in SpectacleGallery. Nothing shows so my question is: Why delete > method is not called when deleting it in that way? > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/django-users/-/6o66VmCVQqMJ. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Method call when deleting FK in admin
My models:class Spectacle(models.Model): title = models.CharField(max_length=100) slug = models.SlugField(max_length=100 unique=False) description = models.TextField() class SpectacleGallery(models.Model): spectacle = models.ForeignKey(Spectacle) image = models.ImageField(upload_to=upload_path_handler, max_length=255, help_text=_(u'tylko pliki z rozszerzeniem .jpg')) image_thumb = models.CharField(max_length=255, editable=False, blank=True, null=True) def delete(self, *args, **kwargs): image_thumb_abs = settings.MEDIA_ROOT + str(self.image_thumb) # try to delete thumb and image try: remove(image_thumb_abs) except Exception: pass try: self.image.delete() except Exception: pass super(SpectacleGallery, self).delete(*args, **kwargs) # Call the "real" delete() method. #try to delete dir try: removedirs(settings.SPECTACLE_GALLERY_UPLOAD_PATH + str(self.spectacle_id)) except: pass Now in admin, when I delete from SpectacleGallery file is being deleted from disk and DB properly. The problem is when I want to delete Spectacle. Deletion page is asking if i'm sure to delete Spectacle and all related images. I confirm and: all records from database are deleted. Unfortunately files from SpectacleGallery are not deleted.I paste print into delete() method in SpectacleGallery. Nothing shows so my question is: Why delete method is not called when deleting it in that way? -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/6o66VmCVQqMJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.