Opened 4 years ago

Closed 2 years ago

#17812 closed Bug (fixed)

Symlink link to media files is deleted after calling of delete method of ImageField if value of it field is None.

Reported by: piterimov@… Owned by: nobody
Component: File uploads/storage Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no


If media folder for media files is symlink then after calling of delete method of ImageField model's field this symbolic link has been deleted if value of this field is None (i. e. value of is empty).
This was found for django 1.3.1 (but maybe and is for others). This is happening for ImageField only. FileField works good.

Attachments (1)

17812.diff (703 bytes) - added by apiterimov 4 years ago.
This is a patch which helped me to solve the problem.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 4 years ago by anonymous

  • Component changed from Uncategorized to File uploads/storage
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 4 years ago by aaugustin

  • Resolution set to worksforme
  • Status changed from new to closed

Here's what I understand from your report (I see you aren't a native speaker):

  • settings.MEDIA_ROOT is a symlink — for instance, /var/www/media -> /mnt/storage/media
  • you save a model that has an ImageField, without providing any value — instance = MyModel.objects.get(pk=1); instance.my_image = None;
  • the symlink gets deleted

I tried that, and I can't reproduce the problem. To be honest, it sounds extremely unlikely that Django would delete a directory or a symlink like that.

comment:3 Changed 4 years ago by anonymous

  • Resolution worksforme deleted
  • Status changed from closed to reopened

It's happening when I try to delete image by image.delete() method.

It's better to show by an example.
Suppose for example we have some model:

class Image(models.Model):
    image = models.ImageField()
    class Meta:
        app_label = 'image'

Then we try to do following:

image = Image()

Thereafter symlink to media files will be deleted.

Also I was mistaken about the FileField : This is happening for it too.

Apparently this is happening because the name of image field is a blank string '' .
Therefore FileSystemStorage.delete method gets path to media symlink and deletes it.

Changed 4 years ago by apiterimov

This is a patch which helped me to solve the problem.

comment:4 Changed 4 years ago by anonymous

  • Has patch set

comment:5 Changed 4 years ago by ramiro

  • Needs tests set
  • Triage Stage changed from Unreviewed to Accepted

comment:6 Changed 3 years ago by aaugustin

  • Status changed from reopened to new

comment:7 Changed 2 years ago by timo

  • Resolution set to fixed
  • Status changed from new to closed

It looks like ea3fe78a9d742904f6902cdc353a11d795418105 will prevent this.

Note: See TracTickets for help on using tickets.
Back to Top