Opened 8 years ago

Closed 7 years ago

Last modified 6 years ago

#5029 closed (duplicate)

newforms FileField special file naming requirements

Reported by: Øyvind Saltvik <oyvind@…> Owned by: nobody
Component: Forms Version: master
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: UI/UX:

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 7 years ago.
add upload_to keyword param to save_FOO_file() method
upload_to.2.diff (2.2 KB) - added by jwickers 7 years ago.
add upload_to keyword param to save_FOO_file() method

Download all attachments as: .zip

Change History (14)

comment:1 Changed 8 years ago by anonymous

  • Cc jm.bugtracking@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 8 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 8 years ago by Simon Litchfield <simon@…>

  • Component changed from Uncategorized to django.newforms
  • Owner changed from jacob to adrian
  • Triage Stage changed from Unreviewed to 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 Changed 8 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 8 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 7 years ago by PhiR

also mentionned in #4113

Changed 7 years ago by jwickers

add upload_to keyword param to save_FOO_file() method

Changed 7 years ago by jwickers

add upload_to keyword param to save_FOO_file() method

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

  • Cc hv@… added

comment:9 Changed 7 years ago by jacob

  • Resolution set to duplicate
  • Status changed from new to closed

This is part of #5361.

comment:10 Changed 7 years ago by faheem

Links to the attachments are currently broken.

comment:11 Changed 7 years ago by Gulopine

  • Keywords fs-rf added

comment:12 Changed 6 years ago by guettli

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