Django substitute old image of ImageFile

217 views
Skip to first unread message

Fabrizio Alongi

unread,
Feb 22, 2014, 8:04:47 AM2/22/14
to django...@googlegroups.com
Hi, I've searched a lot for this problem, but I never found one real solution.

I've a simple Django News model with an ImageField set correctly.
What I need is to delete the old image file when I upload a new image file with ImageField.

What is the best and clear solution?

I've others model have others ImageField... with the same problem.
One solution, maybe, is to clear Media Folder with an automated script... but... I prefer one standalone solution if is possible.


ps: sorry for my english! :)

Fabrizio Alongi

unread,
Feb 22, 2014, 5:51:20 PM2/22/14
to django...@googlegroups.com
Any solutions?? :/

Mike Dewhirst

unread,
Feb 22, 2014, 7:18:07 PM2/22/14
to django...@googlegroups.com
On 23/02/2014 9:51am, Fabrizio Alongi wrote:
> Any solutions?? :/
>

I'm interested too. I have it on my todo list to write a delete routine
for my own software to get rid of uploaded files which are being
replaced. I haven't decided whether to make it automatic or not. I
haven't gotten around to it yet because other things have a higher
priority. If I tackle it I'll post the fix here but if you do it before
me I'd apreciate seeing how you do it.

Cheers

Mike

lokesh

unread,
Feb 24, 2014, 1:52:08 AM2/24/14
to django...@googlegroups.com
Hi,
    When i had a same problem for my imagefield when ever user deletes a image, i use to get the path of the image from the db delete the image record in db and  delete the image via os.system('rm image_path') from media path.

Thanks,
Lokesh

Fabrizio Alongi

unread,
Feb 24, 2014, 7:38:53 AM2/24/14
to django...@googlegroups.com
Hi, after I've searched online, and from this reply  I have wrote this solution by overriding save method of my model class.

def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
        # if an element exist
        if self.id is not None:
            current = MyModelClass.objects.get(id=self.id)
            if self.image != current.image:
                # Delete old image and thumbnail
                current.image.delete(save=False)    # Set save=False because it's saving now.
        super(MyModelClass, self).save(force_insert, force_update, using, update_fields).

Simple!

Mike Dewhirst

unread,
Feb 24, 2014, 5:17:48 PM2/24/14
to django...@googlegroups.com
Fabrizio

Very nice - I'll borrow that for sure :)

Thanks

Mike

On 24/02/2014 11:38pm, Fabrizio Alongi wrote:
> Hi, after I've searched online, and from this reply
> <http://stackoverflow.com/questions/4394194/replacing-a-django-image-doesnt-delete-original#answer-8342249>I
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/d9112e90-fd44-41bc-8710-e0c936082f3c%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages