Opened 17 years ago

Closed 16 years ago

Last modified 16 years ago

#5029 closed (duplicate)

newforms FileField special file naming requirements

Reported by: Øyvind Saltvik <oyvind@…> 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


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)

upload_to.diff (3.9 KB ) - added by jwickers 17 years ago.
add upload_to keyword param to save_FOO_file() method
upload_to.2.diff (2.2 KB ) - added by jwickers 17 years ago.
add upload_to keyword param to save_FOO_file() method

Download all attachments as: .zip

Change History (14)

comment:1 by anonymous, 17 years ago

Cc: jm.bugtracking@… added

comment:2 by ross@…, 17 years ago

Cc: ross@… added

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.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?

comment:3 by Simon Litchfield <simon@…>, 17 years ago

Component: Uncategorizeddjango.newforms
Owner: changed from Jacob to Adrian Holovaty
Triage Stage: UnreviewedDesign 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 Simon Litchfield <simon@…>, 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 ross@…, 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.

comment:6 by Philippe Raoult, 17 years ago

also mentionned in #4113

by jwickers, 17 years ago

Attachment: upload_to.diff added

add upload_to keyword param to save_FOO_file() method

by jwickers, 17 years ago

Attachment: upload_to.2.diff added

add upload_to keyword param to save_FOO_file() method

comment:7 by jwickers, 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" %(

comment:8 by Thomas Güttler <hv@…>, 17 years ago

Cc: hv@… added

comment:9 by Jacob, 16 years ago

Resolution: duplicate
Status: newclosed

This is part of #5361.

comment:10 by Faheem Mitha, 16 years ago

Links to the attachments are currently broken.

comment:11 by Marty Alchin, 16 years ago

Keywords: fs-rf added

comment:12 by Thomas Güttler, 16 years ago

Cc: hv@… removed
Note: See TracTickets for help on using tickets.
Back to Top