On Tue, 2007-10-23 at 08:49 -0400, Marty Alchin wrote: > In response to some recent questions regarding FileField usage, I > thought about including a way to format the filename based on details > from the model instance itself. It's looking like it' be best to add > an argument to FileField, called 'filename' perhaps, which takes a > format string, like so (pardon the inevitable line munging): > > class Song(models.Model): > album = models.CharField(maxlength=255) > title = models.CharField(maxlength=255) > artist = models.CharField(maxlength=255) > track = models.PositiveSmallIntegerField() > year = models.PositiveIntegerField() > genre = models.CharField(maxlength=40) > file = models.FileField(upload_to='songs', > filename='%(album)s_%(track)s_%(title)s.mp3') > > However, this raises two concerns, both stemming from the fact that > given people a cookie will make them want a glass of milk: > > * Many (though I expect not all) will believe that the filename would > update automatically when the model itself is updated. This is > obviously not the case, and the documentation would try to make this > clear, but it's a whole round of questions I expect to hear anyway. > > * An often-requested variant on this scheme is the ability to > determine subdirectories based on instance data as well. For instance: > > ..., > filename='%(genre)s/%(year)s/%(artist)s/%(album)s_%(track)s_%(title)s.mp3' > > Currently, get_filename uses os.path.basename, and after filestorage > goes in, get_available_filename will do the same. This means that any > directory information is lost, unless it's preserved separately. It's > possible, but will take a bit of doing.
I don't understand this paragraph. Currently, if you use upload_to, we store the full upload_to prefix + the filename (with the implicit assumption that it's all appended to MEDIA_ROOT), both in the database field and in the model attribute. Why wouldn't you do the same -- store the full relative path -- in the new world? What am I missing. Unless you allow subdirectory support, I doubt it's worth adding much customisation here, since it's not going to be too useful. My impression has been that allowing customisation inside a Model.save() override is the real wish -- so rather than having to specify upload_to (or filename in your case) up-front, it can be changed a bit more dynamically just prior to saving. Malcolm -- On the other hand, you have different fingers. http://www.pointy-stick.com/blog/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-developers?hl=en -~----------~----~----~----~------~----~------~--~---