#5029 closed (duplicate)
newforms FileField special file naming requirements
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Keywords: | file naming newforms filefield, fs-rf | |
Cc: | jm.bugtracking@…, ross@… | Triage Stage: | Design decision needed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Would be nice if the FileField allowed files to be saved based on other attributes on the model.
This requires setting those attributes before the save_form_data (new in #3297) is called by the form to save the file.
The best would be to make this a spechial case that handles those fields last, not only FileFields but any field that requires other model data.
If the saving needs a pk, so the filename/dir of the file is based on the model instance pk. save_form_data shold be called right after commit and save the model instance again. Preferably once and not one save per field.
The problem with saving twice is if the field has null=False the insert would fail because of missing data on the instance, the solution would be to handle the field both before and after commit. Setting the attribute to before the insert, then saving the file on the update.
Attachments (2)
Change History (14)
comment:1 by , 17 years ago
Cc: | added |
---|
comment:2 by , 17 years ago
Cc: | added |
---|
comment:3 by , 17 years ago
Component: | Uncategorized → django.newforms |
---|---|
Owner: | changed from | to
Triage Stage: | Unreviewed → Design decision needed |
Yes I think that would be nice, but we don't have access to self there - so maybe --
attachment = models.FileField(upload_to='uploads/%s/' % self.project, filename='%(id)s-%(filename)s.%(ext)s')
The string expansion dict used could have the models' fields plus special filename and ext for the original filenames. Think we might want to urlize the filenames (lowercase, no punct) before expanding too.
comment:4 by , 17 years ago
Ooops, reading again I see we might also want to expand the upload_to string --
attachment = models.FileField?(upload_to='uploads/%(project)s/', filename='%(id)s-%(filename)s.%(ext)s')
comment:5 by , 17 years ago
Think we might want to urlize the filenames (lowercase, no punct) before expanding too.
This is a good idea but should be over-ridable. Consider perhaps this example:
attachment = models.FileField(upload_to='uploads/%(project)s/%(id)s/', filename='%(filename_original)')
Or, perhaps, leave out the file name option. This would create uploads/ABC123/481/Important Spreadsheet from the CEO.xls
Leads me to think that the exact same options should be available in upload_to
and filename
, and leaving them out leaves behaviour as it currently is - eg upload_to
required, filename
defaults to the original name of the uploaded file.
by , 17 years ago
Attachment: | upload_to.diff added |
---|
add upload_to keyword param to save_FOO_file() method
by , 17 years ago
Attachment: | upload_to.2.diff added |
---|
add upload_to keyword param to save_FOO_file() method
comment:7 by , 17 years ago
For now i use the above patch (sorry for double posting, the first one had a hack for i18n i forgot about ..)
I can then specify the new upload_to in the view:
my_model.save_FOO_file(filename, content, upload_to="myfolder/%s" %(my_model.id))
comment:8 by , 17 years ago
Cc: | added |
---|
comment:11 by , 17 years ago
Keywords: | fs-rf added |
---|
comment:12 by , 16 years ago
Cc: | removed |
---|
Presently a FileField can have it's directory changed using date %m%d%y commands, it'd be great to be able to do something similar to the filename. For example:
attachment = models.FileField(upload_to='uploads/%s/' % self.project, filename='%s-%s.%s' % (self.id, self.attachment.original_filename, self.attachment.extension))
This would require extra attributes for each FileField, for the original filename and the extension, which may beyond the scope of this task... what thoughts do others have?