Opened 16 years ago

Closed 16 years ago

Last modified 15 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

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)

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

Download all attachments as: .zip

Change History (14)

comment:1 Changed 16 years ago by anonymous

Cc: jm.bugtracking@… added

comment:2 Changed 16 years ago by ross@…

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

comment:3 Changed 16 years ago by Simon Litchfield <simon@…>

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 Changed 16 years ago by Simon Litchfield <simon@…>

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 Changed 16 years ago by ross@…

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 Changed 16 years ago by Philippe Raoult

also mentionned in #4113

Changed 16 years ago by jwickers

Attachment: upload_to.diff added

add upload_to keyword param to save_FOO_file() method

Changed 16 years ago by jwickers

Attachment: upload_to.2.diff added

add upload_to keyword param to save_FOO_file() method

comment:7 Changed 16 years ago by jwickers

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 Changed 16 years ago by Thomas Güttler <hv@…>

Cc: hv@… added

comment:9 Changed 16 years ago by Jacob

Resolution: duplicate
Status: newclosed

This is part of #5361.

comment:10 Changed 16 years ago by Faheem Mitha

Links to the attachments are currently broken.

comment:11 Changed 16 years ago by Marty Alchin

Keywords: fs-rf added

comment:12 Changed 15 years ago by Thomas Güttler

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