Opened 8 years ago

Closed 8 years ago

#6714 closed (duplicate)

let update_to accept a callable

Reported by: Griffith Rees <griff.rees@…> Owned by: nobody
Component: Core (Other) Version: master
Severity: Keywords: FileField Models
Cc: andrew@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


It would be nice if the upload_to parameter on FileFields accepted a callable as an argument. It could be called with the model instance as its only variable the first time the instance needed to be saved to disk (afterwards, it should rely on the database entry for the path).

Currently, it only accepts a formatting string, which means you cannot, for example, upload files to directories based on their filesize or other data (such as other attributes of the model instance).

class IssuePrintEdition(model.Model):
    issue = ForeignKey(Issue) # The issue this print edition is related to
    section = ForeignKey(Section)
    file = FileField(upload_to=upload_path)
def upload_path(instance):
    return "issues/%s/%s" % (instance.issue.title, instance.section)

Change History (1)

comment:1 Changed 8 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

There's a whole bunch of design work going on in this area. Have a look in the archives of the django-developers list and in #5361 (which I'm going to mark this ticket as a duplicate of, since it's subsumed into that). It's not a trivial change for a number of reasons, but it's work in progress as part of a larger ongoing effort.

Note: See TracTickets for help on using tickets.
Back to Top