Django

Code

Ticket #4979 (reopened)

Opened 2 years ago

Last modified 5 months ago

Admin does not allow removal of an image from an ImageField after it has been set, even if null=True and blank=True

Reported by: robvdl Assigned to: nobody
Milestone: Component: django.contrib.admin
Version: SVN Keywords: ImageField
Cc: robillard.etienne@gmail.com Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 1

Description

In my model, I have the following field:

photo = models.ImageField("Photo", upload_to="images", null=True, blank=True)

I am able to upload an image using the admin tool no problem, I am also able to change it to another image at a later stage, but I am not able to remove the image (e.g. set the field back to NULL), even though my field does allow null values. This might also apply to FileField fields.

Attachments

4979-admin-file-deletion.diff (3.9 kB) - added by Daniel Pope <dan@mauveinternet.co.uk> on 12/15/08 18:14:17.
Patch for admin filefield deletion
4979-admin-file-deletion.2.diff (4.1 kB) - added by Daniel Pope <dan@mauveinternet.co.uk> on 12/15/08 18:37:59.
Admin filefield deletion (updated for style)

Change History

07/27/07 07:18:58 changed by mariarchi

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

As far as I know, image removal is just not yet implemented in built-in admin. There was a solution around on how to fix it in wiki on this website (checkbox "delete" appears near each image).

09/08/07 12:08:01 changed by anonymous

  • cc set to robillard.etienne@gmail.com.

09/16/07 15:22:25 changed by ubernostrum

  • status changed from new to closed.
  • resolution set to duplicate.

Duplicate of #2983.

06/16/08 12:02:18 changed by rodrigo.cr@gmail.com

  • status changed from closed to reopened.
  • resolution deleted.

This is not a duplicate of 2983. The issue here is setting a previously filled in ImageField? (or FileField?) to blank, not the automated deletion of files when new ones are uploaded.

06/17/08 11:59:56 changed by programmerq

  • stage changed from Unreviewed to Accepted.

12/15/08 18:14:17 changed by Daniel Pope <dan@mauveinternet.co.uk>

  • attachment 4979-admin-file-deletion.diff added.

Patch for admin filefield deletion

12/15/08 18:30:28 changed by Daniel Pope <dan@mauveinternet.co.uk>

  • needs_better_patch set to 1.
  • has_patch set to 1.

I've added an initial patch - needs a bit of cleaning up as I've made a bit of a mess wrt PEP8 so I will reupload in a moment.

In this patch I've taken the approach of False indicating a file explicitly unset, None indicating no new value. I don't particularly like that approach, but FileField is special-cased at the model, form and form field level so it defaults to preserving the current value. A cleaner approach might be to remove the special case that makes it default to preserving a value, and have FileField explicitly return the same file. But that would mean more upheaval, less compatibility.

12/15/08 18:37:59 changed by Daniel Pope <dan@mauveinternet.co.uk>

  • attachment 4979-admin-file-deletion.2.diff added.

Admin filefield deletion (updated for style)

12/15/08 18:38:47 changed by Daniel Pope <dan@mauveinternet.co.uk>

  • needs_better_patch deleted.

02/16/09 15:08:46 changed by jacob

  • needs_better_patch set to 1.

This aproach is fine with me, but overloading False to mean "delete the file" smells bad to me. A relatively easy change that'd remove (some of) the smell would be to use a sentinel object (i.e. DeleteFile = object()) instead of False.

02/17/09 06:28:03 changed by Daniel Pope <dan@mauveinternet.co.uk>

To offer as much compatibility as possible for anyone using ModelAdmin hooks I thought it best to preserve the ability to do

if form.cleaned_data['file']:

So I suppose that could be achieved like this

class DeleteFileType(object):
    def __nonzero__(self):
        return False

DeleteFile = DeleteFileType()

02/19/09 14:59:18 changed by jacob

You know, maybe we're over-thinking this a bit. Why not leave cleaned_data['file'] alone, and just deal with the checkbox as a separate bit of data (cleaned_data['__delete_file'] or somesuch)? Anyone using ModelAdmin hooks will need to update 'em anyway to deal with the new features, so there's not a big change here.

02/19/09 15:05:57 changed by Alex

Jacob: unless I'm missing something I think there's a second ticket(in the 7000s I think) that uses a MultiValueField? to pretty much do that.

02/19/09 15:08:07 changed by Alex

#7048 for reference.


Add/Change #4979 (Admin does not allow removal of an image from an ImageField after it has been set, even if null=True and blank=True)




Change Properties
Action