Re: managing files, deleting file from file system with admin site

79 views
Skip to first unread message

m1chael

unread,
Nov 15, 2012, 8:46:59 AM11/15/12
to django...@googlegroups.com
I think you need to do this with a signal, or on post_save

On Thu, Nov 15, 2012 at 12:48 AM, Sergey Seleznev <art...@gmail.com> wrote:
> Hi! I'm new to python and django(well, and web dev too).
> I'm using python 2.7 and django 1.4
> I went through the tutorial and then got issue with files managing.
> I have model like this:
>
> class Car(models.Model):
> #some fields
> photo = models.ImageField(upload_to='cars')
>
> and media url/root configured in settings.py.
> Also I have autogenerated django admin site.
> Its works well and I can upload files to MEDIA_ROOT/cars/ automaticaly when
> adding new object to Car model, but when I delete or change that object from
> admin site old file doesnt deletes.
> I need some easy and a good way to configure model/admin_site/something_else
> to delete old files from file system when i change/delete model object from
> admin site.
> I think admin actions can be solution, but dont think it helps in case of
> changes and it cant help override "Delete" button from object edit page.
>
> Thanks for your help!
>
> --
> 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/-/86NC9uiQ4uAJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

lingrlongr

unread,
Nov 15, 2012, 9:02:02 AM11/15/12
to django...@googlegroups.com
I've use something like this:

class Car(models.Model):
    #some fields
    photo = models.ImageField(upload_to='cars')

    def delete(self, *args, **kwargs):
        storage, path = self.photo.storage, self.photo.path
        super(Car, self).delete(*args, **kwargs)
        storage.delete(path)

Sergey Seleznev

unread,
Nov 15, 2012, 7:55:59 PM11/15/12
to django...@googlegroups.com
Thanks! Both methods are useful.

четверг, 15 ноября 2012 г., 18:02:02 UTC+4 пользователь lingrlongr написал:

Sergey Seleznev

unread,
Nov 15, 2012, 11:15:18 PM11/15/12
to django...@googlegroups.com
I tried to override delete() method of Car model and it works when i delete single object using admin site.
But it doesnt work with deleting many objects at once.
So i ended up with this:

def post_delete_image_deleting(sender, instance, **kwargs):
    storage, path = instance.photo.storage, instance.photo.path
    storage.delete(path)

def pre_save_image_deleting(sender, instance, **kwargs):
    try:
        old_instance = Car.objects.get(pk = instance.pk)
    except:
        pass
    else:
        if instance.photo != old_instance.photo:
            storage = old_instance.photo.storage
            path = old_instance.photo.path
            storage.delete(path)

post_delete.connect(post_delete_image_deleting, sender=Car)
pre_save.connect(pre_save_image_deleting, sender=Car)

It works for me.
Reply all
Reply to author
Forward
0 new messages