Login

Add a "remove file" field for Image- or FileFields

Author:
rodrigoc
Posted:
July 18, 2008
Language:
Python
Version:
.96
Tags:
image remove file
Score:
3 (after 3 ratings)

This adds a checkbox in the admin site that removes the reference to a file uploaded via a FileField (or ImageField). It does not delete the actual file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
    the_file = models.FileField(
        upload_to="file/path",
        blank=True)
    remove_the_file = models.BooleanField()

    def save(self):
        if self.remove_the_file:
            self.the_file = ""
            self.remove_the_file = False
        super(ModelName, self).save()

More like this

  1. Updated FileField / ImageField with a delete checkbox by tomZ 8 years, 5 months ago
  2. FileField / ImageField with a delete checkbox by tomZ 8 years, 9 months ago
  3. Integrate Wymeditor with filebrowser plugin by jonasvp 7 years, 9 months ago
  4. Simple "html email with images" sender by andres_torres_marroquin 5 years, 6 months ago
  5. Admin Image Widget by baumer1122 8 years ago

Comments

jerry2801 (on October 21, 2009):
<p>is a good idea~</p>

#

rfugger (on November 3, 2009):
<p>This is simple and nice. I prefer to not add an extra database column when it's not needed, though. You can just create a custom ModelForm for your model, with the following:</p> <pre>remove_the_file = forms.BooleanField(required=False) def save(self, *args, **kwargs): object = super(self.__class__, self).save(*args, **kwargs) if self.cleaned_data.get('remove_the_file'): object.the_file = '' return object </pre> <p>Use that form in your ModelAdmin, and there's no need to change the database.</p> <p>Thanks for the idea!</p>

#

hede (on November 27, 2010):
<p>Seems something has changed. There's some "commit" parameter. If it's set, above things do not work here. Btw: I cannot use self.__class__ here neither. For me the following works (SectionModelForm is the ModelForm class):</p> <pre>remove_file = forms.BooleanField(required=False) def save(self, commit=False, *args, **kwargs): obj = super(SectionModelForm, self).save(commit=False, *args, **kwargs) if self.cleaned_data.get('remove_file'): obj.file = '' if commit: obj.save() return obj </pre>

#

mhulse (on May 20, 2011):
<p>Just FYI for those thinking about using code mentioned here...</p> <p>This adds a checkbox to the model, in the admin, at the bottom of the form. This may not be optimal if you have multiple file fields in your model.</p> <p>Optimally, I would prefer to have the checkbox next to the file form field itself.</p>

#

sdeleon28 (on October 19, 2011):
<p>"Optimally, I would prefer to have the checkbox next to the file form field itself."</p> <p>This is default behavior when the FileField is set as optional in the model.</p> <p>The trick is to set both blank=True, null=True.</p> <p>Don't use the snippet, define the model correctly! ;) Hope this helps.</p>

#

machrider (on October 25, 2011):
<p>Just to clarify sdeleon28's comment:</p> <p>The blank=True, null=True feature was added in Django 1.3. FileFields now use a ClearableFileInput widget (added in 1.3) by default. If you're on Django 1.2 or earlier, you'll still need to do some customization yourself.</p>

#

Please login first before commenting.