Opened 17 years ago
Closed 14 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)
Change History (61)
comment:1 by , 17 years ago
Keywords: | fs-rf added |
---|
comment:2 by , 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 , 16 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:4 by , 16 years ago
Keywords: | yandex-sprint added |
---|
comment:5 by , 16 years ago
Cc: | 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 , 16 years ago
milestone: | → 1.0 |
---|---|
Triage Stage: | Unreviewed → Accepted |
Version: | newforms-admin → SVN |
comment:7 by , 16 years ago
Cc: | added |
---|
comment:8 by , 16 years ago
Keywords: | nfa-someday yandex-sprint removed |
---|---|
Owner: | changed from | to
Status: | assigned → new |
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:10 by , 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
Attachment: | DeletableFileFields-00_code.diff added |
---|
First version of the patch, no test fixes yet, docs are separate
by , 16 years ago
Attachment: | DeletableFileFields-00_docs.txt added |
---|
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 , 16 years ago
Cc: | added |
---|
comment:14 by , 16 years ago
milestone: | 1.0 → post-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 , 16 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 , 16 years ago
Cc: | added |
---|
by , 16 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 , 16 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 , 16 years ago
Attachment: | DeletableFileFields-02.diff added |
---|
Moved documentation to the correct file
comment:19 by , 16 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 , 16 years ago
Cc: | added |
---|
comment:21 by , 16 years ago
Cc: | added |
---|
comment:22 by , 16 years ago
Cc: | added |
---|
comment:23 by , 16 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:24 by , 16 years ago
Cc: | added |
---|
comment:25 by , 16 years ago
Summary: | Support clearing FileFields in the admin if blank=True → Support 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 , 16 years ago
(Oh and forgot to mention assuming the wider scope, #10299 has been closed as a dupe.)
comment:27 by , 16 years ago
Cc: | added |
---|
comment:29 by , 16 years ago
Cc: | 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 , 16 years ago
milestone: | → 1.1 |
---|
Seems to be in scope for 1.1 according to Version1.1Features.
comment:31 by , 16 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 , 16 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 , 16 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 , 16 years ago
milestone: | 1.1 → 1.2 |
---|
comment:35 by , 15 years ago
Cc: | added |
---|
comment:36 by , 15 years ago
Cc: | added |
---|
comment:37 by , 15 years ago
Cc: | added |
---|
comment:38 by , 15 years ago
Cc: | added |
---|
comment:39 by , 15 years ago
Cc: | added |
---|
comment:40 by , 15 years ago
Cc: | added |
---|
comment:41 by , 15 years ago
Cc: | added |
---|
comment:42 by , 15 years ago
Cc: | added |
---|
comment:43 by , 15 years ago
Cc: | added |
---|
comment:44 by , 15 years ago
milestone: | 1.2 |
---|
1.2 is feature-frozen, moving this feature request off the milestone.
comment:45 by , 14 years ago
Cc: | added |
---|
comment:46 by , 14 years ago
Cc: | added |
---|
comment:47 by , 14 years ago
Owner: | changed from | to
---|---|
Status: | assigned → new |
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 , 14 years ago
Component: | django.contrib.admin → Forms |
---|
by , 14 years ago
Attachment: | 7048_r13753.diff added |
---|
comment:49 by , 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 , 14 years ago
Needs tests: | unset |
---|---|
Status: | new → assigned |
comment:51 by , 14 years ago
Keywords: | sprintSep2010 added |
---|
by , 14 years ago
Attachment: | 7048_r13817.diff added |
---|
use hyphenated clear-checkbox name to better avoid name collisions
comment:52 by , 14 years ago
Cc: | added |
---|
comment:53 by , 14 years ago
Cc: | added |
---|
comment:54 by , 14 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
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.