Opened 16 years ago

Closed 13 years ago

#7048 closed (fixed)

Support clearing FileFields with ModelForms

Reported by: Owned by: Carl Meyer
Component: Forms Version: dev
Severity: Keywords: sprintSep2010
Cc: anball@…, code@…, david@…, msaelices@…, tahooie@…, digitaljhelms@…, andy@…, taavi@…, flosch@…, valera.grishin@…, liangent@…, nicola.murino@…, michelts@…, jdunck@…, drmeers@…, sebleier@…, herbert.poul@…, flavio.curella@…, aribao@…, dan.fairs@…, tjurewicz@…, mortas.11@…, brent.tubbs@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently one can set blank=True in the model. This works in the admin: It let's you save entries without the file. But once you submit a file, you cannot get rid of it!

So blank=True on FileFields currently means "blank first, then set forever". Which is a not the most common use case IMHO ;)

There already is a snippet implementing a RemovableFileField: http://www.djangosnippets.org/snippets/636/

If the deletion of the file from disk isn't desired I would like to at least contribute a patch to clear the field. I'm filing this for newforms-admin so it doesn't get outdated right away.

Any thoughts on this are greatly appreciated!

Attachments (7)

DeletableFileFields-00_code.diff (17.7 KB ) - added by 16 years ago.
First version of the patch, no test fixes yet, docs are separate
DeletableFileFields-00_docs.txt (1.2 KB ) - added by 16 years ago.
Docs as a simple text file as the docs refactor is going to land anyway …
DeletableFileFields-01.diff (19.5 KB ) - added by 15 years ago.
Updated patch to r9381, included docs, checking blank=True before deleting (against forged POST data)
DeletableFileFields-02.diff (19.6 KB ) - added by 15 years ago.
Moved documentation to the correct file
7048_r13753.diff (26.8 KB ) - added by Carl Meyer 14 years ago.
7048_r13817.diff (26.8 KB ) - added by Carl Meyer 14 years ago.
use hyphenated clear-checkbox name to better avoid name collisions
7048_r13926.diff (26.8 KB ) - added by Jannis Leidel 13 years ago.
UI updates and a few other issues fixed

Download all attachments as: .zip

Change History (61)

comment:1 by Marc Garcia, 16 years ago

Keywords: fs-rf added

I think that I asked it in the developers list, and the idea is not spend time in FileFields because they are being refactored. Marking this ticket as fs-rf to be considered.

comment:2 by Marty Alchin, 16 years ago

Keywords: fs-rf removed

The filestorage refactor does indeed make it easier to delete files from within Python, but it doesn't extend to the admin interface at all. Removing the fs-rf keyword.

comment:3 by Mikhail, 16 years ago

Owner: changed from nobody to Mikhail
Status: newassigned

comment:4 by Ivan Sagalaev, 16 years ago

Keywords: yandex-sprint added

comment:5 by Jonas von Poser, 16 years ago

Cc: code@… added

I'm very interested in a solution to this problem and would love to help out. Let me know if I can do something.

comment:6 by Jacob, 16 years ago

milestone: 1.0
Triage Stage: UnreviewedAccepted
Version: newforms-adminSVN

comment:7 by David Reynolds, 16 years ago

Cc: david@… added

comment:8 by , 16 years ago

Keywords: nfa-someday yandex-sprint removed
Owner: changed from Mikhail to
Status: assignednew

Claiming the ticket as there hasn't been any activity in the last month. If there exists a patch from the Yandex sprint now would be a good time to post it :)

comment:9 by Ivan Sagalaev, 16 years ago

Looks like we didn't get to it after all. Feel free to take it, thanks!

comment:10 by Marc Garcia, 16 years ago

I don't have a patch for it, but this feature is working in a field that subclasses FileField on the stdimage application. So it should be easy to extract from there, and create a patch. Probably the patch will need to be improved later, but at least is a starting point.

stdimage is available at http://code.google.com/p/django-stdimage/

comment:11 by , 16 years ago

Cool, thx! I also got code from brosner. I will probably use both sources to compile a patch :)

by , 16 years ago

First version of the patch, no test fixes yet, docs are separate

by , 16 years ago

Docs as a simple text file as the docs refactor is going to land anyway ...

comment:12 by , 16 years ago

Has patch: set
Needs tests: set

After talking to brosner on #django-dev we decided to implement this on the ModelForm level. The attached patch introduces file deletion functionality for ModelForms which can be customized via its Meta class (see attached docs).

Work still to be done:

  • Check admin CSS
    • at least for the RTL design this is bound to break
    • what about inlines?
  • Fix regression tests
  • Write new tests ;)

Notes on the patch:

  • I had to add some special admin CSS to align everything properly. That can only be achieved with a fixed padding to the left ...
  • For the DeletableFileField I started with a MultiValueField till i had overwritten every piece of code in it ... now it just sublcasses Field. The widget still subclasses MultiWidget though.
  • The display of the current file moved from the admin to ModelForm. It just doesn't make sense to provide a delete option if the presence of a file isn't visible ;)

If I missed anything in this short description let me know. Please test and tell me what breaks I'll keep the patch up to date :)

comment:13 by Manuel Saelices, 16 years ago

Cc: msaelices@… added

comment:14 by Brian Rosner, 16 years ago

milestone: 1.0post-1.0

After seeing what is really involved in this ticket. jarrow and I decided it is best to avoid this pre-1.0. There is just too much design decisions that need to be made to make this work the right way. I personally appreciate jarrow's work. This will certainly make it post-1.0. :)

comment:15 by , 16 years ago

Just a note to myself: Maybe the CSS could be shorter as I introduced <br/>s after writing the style rules. Haven't checked though. The <br/>s are there for a sensible default display without CSS when using for example as_table().

comment:16 by , 15 years ago

#6405 is about making MultiValueField pass through initial data. Maybe this could be closed if a deletable FileField is the only use case.

comment:17 by tahooie, 15 years ago

Cc: tahooie@… added

by , 15 years ago

Attachment: DeletableFileFields-01.diff added

Updated patch to r9381, included docs, checking blank=True before deleting (against forged POST data)

comment:18 by , 15 years ago

The attached patch seems to work fine. The tests still need fixing though. I wanted to wait for some feedback on the code and approach :) RTL CSS also not done yet. But there seems to be an admin redesign forthcoming anyway.

by , 15 years ago

Attachment: DeletableFileFields-02.diff added

Moved documentation to the correct file

comment:19 by , 15 years ago

With the current default values for the ModelForm Meta class options the admin shows the delete checkbox and removes the file from disk. It does not remove empty directories. To enable this in the admin one currently needs to do something like this:

class TestModelAdmin(admin.ModelAdmin):
    def get_form(self, request, obj=None, **kwargs):
        defaults = {
            "files_delete_empty_dirs": True,
        }
        defaults.update(kwargs)
        return super(TestModelAdmin, self).get_form(request, obj, **defaults)

Maybe a settings option to make deletion of empty dirs the default would be nice. Or we could add a files_delete_empty_dirs option to the ModelAdmin class and pass it to the ModelForm (like we do with exclude etc.).

comment:20 by digitaljhelms, 15 years ago

Cc: digitaljhelms@… added

comment:21 by Andy Baker, 15 years ago

Cc: andy@… added

comment:22 by anonymous, 15 years ago

Cc: taavi@… added

comment:23 by Brian Rosner, 15 years ago

Owner: changed from to Brian Rosner
Status: newassigned

comment:24 by anonymous, 15 years ago

Cc: flosch@… added

comment:25 by Karen Tracey, 15 years ago

Summary: Support clearing FileFields in the admin if blank=TrueSupport clearing FileFields with ModelForms

Updated summary to reflect scope of the ticket -- discussion and patch indicate the solution here will not be admin-specific.

comment:26 by Karen Tracey, 15 years ago

(Oh and forgot to mention assuming the wider scope, #10299 has been closed as a dupe.)

comment:27 by Valera_Grishin, 15 years ago

Cc: valera.grishin@… added

comment:28 by (none), 15 years ago

milestone: post-1.0

Milestone post-1.0 deleted

comment:29 by liangent, 15 years ago

Cc: liangent@… added

a problem...

if I created a subclass of ModelForm, then added some FileField(s) which are not in the Model, the latest patch will raise FieldDoesNotExist: Xxx has no field named 'xxx' .

comment:30 by mrts, 15 years ago

milestone: 1.1

Seems to be in scope for 1.1 according to Version1.1Features.

comment:31 by Jacob, 15 years ago

It looks like this patch metastasized and grew some unrelated features -- to wit, nothing about this ticket has anything to do with deleting files (?) and directories (!) as a side-effect of clearing a file field. That's a separate discussion that we haven't had yet, and we're certainly not sneaking in new behavior like that this late in the release cycle.

comment:32 by , 15 years ago

The deletion of files from disk was already in the code that Brian gave me. So I thought that it was in scope for the ticket. I also wouldn't call the deletion of files and directories unrelated. I would expect the admin to be able to get rid of uploaded files if it has the option to clear the field. But we could of course disable this by default. Sure the delete directories feature sounds scary, that's why it is off by default.

Brian wanted to review the ticket. I hope he finds the time to have a look at this (he seems to be rather busy at the moment ...).

comment:33 by Alex Gaynor, 15 years ago

I'd like to suggest, in the strongest terms possible, that this shouldn't be a Meta option, it should be an option on the Field. Meta is for formwide options.

comment:34 by Alex Gaynor, 15 years ago

milestone: 1.11.2

comment:35 by anonymous, 15 years ago

Cc: nicola.murino@… added

comment:36 by anonymous, 15 years ago

Cc: anball@… added

comment:37 by Michel Sabchuk, 15 years ago

Cc: michelts@… added

comment:38 by anonymous, 15 years ago

Cc: jdunck@… added

comment:39 by Simon Meers, 15 years ago

Cc: drmeers@… added

comment:40 by anonymous, 14 years ago

Cc: sebleier@… added

comment:41 by Herbert Poul, 14 years ago

Cc: herbert.poul@… added

comment:42 by Flavio Curella, 14 years ago

Cc: flavio.curella@… added

comment:43 by Adrian Ribao, 14 years ago

Cc: aribao@… added

comment:44 by James Bennett, 14 years ago

milestone: 1.2

1.2 is feature-frozen, moving this feature request off the milestone.

comment:45 by Dan Fairs, 14 years ago

Cc: dan.fairs@… added

comment:46 by trent, 14 years ago

Cc: tjurewicz@… added

comment:47 by Carl Meyer, 14 years ago

Owner: changed from Brian Rosner to Carl Meyer
Status: assignednew

Picking this up at the sprints. Going to rework the patch to remove file-deletion entirely (that can be a separate ticket/discussion) and keep things out of the form Meta per Alex's comment.

comment:48 by Carl Meyer, 14 years ago

Component: django.contrib.adminForms

by Carl Meyer, 14 years ago

Attachment: 7048_r13753.diff added

comment:49 by Carl Meyer, 14 years ago

Added a new patch with these design choices:

  • No file deleting.
  • Clearability should be the default behavior of a forms.FileField(required=False) - and thus also models.FileField(blank=True).formfield(). It's a bug that FileFields are currently not clearable. Thus, no Meta options or extra field arguments are needed.
  • This involves a minor backwards incompatibility in form rendering (a possible checkbox where there was not one previously), which is documented in the 1.3 release notes.
  • It doesn't make sense to have a clearable file input that doesn't display the current value of the field, so that behavior is moved from AdminFileWidget to ClearableFileInput (which AdminFileWidget inherits).
  • The HTML rendering of the ClearableFileInput is customizable by subclassing and overriding some class attributes.
  • Contradictory input (checking the clear checkbox and simultaneously uploading a new file) results in a form validation error.

All of these design choices were checked in person with at least one core committer at the sprint, mostly with Malcolm.

For reviewers more comfortable with git, here is the github compare URL for my working branch for this patch: http://github.com/carljm/django/compare/master...ticket_7048

comment:50 by Carl Meyer, 14 years ago

Needs tests: unset
Status: newassigned

comment:51 by Carl Meyer, 14 years ago

Keywords: sprintSep2010 added

by Carl Meyer, 14 years ago

Attachment: 7048_r13817.diff added

use hyphenated clear-checkbox name to better avoid name collisions

comment:52 by Althalus, 13 years ago

Cc: mortas.11@… added

comment:53 by btubbs, 13 years ago

Cc: brent.tubbs@… added

by Jannis Leidel, 13 years ago

Attachment: 7048_r13926.diff added

UI updates and a few other issues fixed

comment:54 by Jannis Leidel, 13 years ago

Resolution: fixed
Status: assignedclosed

(In [13968]) Fixed #7048 -- Added ClearableFileInput widget to clear file fields. Thanks for report and patch, jarrow and Carl Meyer.

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