Login

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

Author:
rodrigoc
Posted:
July 18, 2008
Language:
Python
Version:
.96
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. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

jerry2801 (on October 21, 2009):

is a good idea~

#

rfugger (on November 3, 2009):

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:

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

Use that form in your ModelAdmin, and there's no need to change the database.

Thanks for the idea!

#

hede (on November 27, 2010):

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):

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

#

mhulse (on May 20, 2011):

Just FYI for those thinking about using code mentioned here...

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.

Optimally, I would prefer to have the checkbox next to the file form field itself.

#

sdeleon28 (on October 19, 2011):

"Optimally, I would prefer to have the checkbox next to the file form field itself."

This is default behavior when the FileField is set as optional in the model.

The trick is to set both blank=True, null=True.

Don't use the snippet, define the model correctly! ;) Hope this helps.

#

machrider (on October 25, 2011):

Just to clarify sdeleon28's comment:

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.

#

Please login first before commenting.