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
- Updated FileField / ImageField with a delete checkbox by tomZ 5 years, 2 months ago
- FileField / ImageField with a delete checkbox by tomZ 5 years, 6 months ago
- Removing old ImageFields and FileFields when updating through admin by alejandro.alonso 1 year ago
- Clear FileField/ImageField files in the Admin by marinho 3 years, 10 months ago
- File deletion of FileField and ImageField in the Admin panel by davmuz 10 months, 3 weeks ago
Comments
is a good idea~
#
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:
Use that form in your ModelAdmin, and there's no need to change the database.
Thanks for the idea!
#
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):
#
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.
#
"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.
#
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.
#