Uploading files to subdirectories

5 views
Skip to first unread message

James McGill

unread,
May 16, 2008, 3:00:04 AM5/16/08
to Django users
Hi All,
I'm new to Django and I'm not sure how best to achieve the following:

Users may upload files - each uploaded file is tied to a user in the
database using a ForeignKey.

In order to prevent pain to the underlying O/S I don't want to put all
these files in to a single directory. I could separate the files by
upload time as mentioned in the Django documentation, but it would be
nicer to have all the files uploaded by a single user under a single
subdirectory.

Example:
Greg uploads "holiday.rtf", it is saved as /media/uploads/greg/
holiday.rtf
Sally uploads "family.rtf", it is saved as /media/uploads/sally/
family.rtf

My current model:

class FileModel(models.Model):
user = models.ForeignKey(User)
file = models.FileField(upload_to = 'uploads/'

I am saving the file using FileModel_Instance.save_file_file('path',
file_contents). Adding extra folders to the path (i.e. path = greg/
holiday.rtf) doesn't work - I assume this is a security restriction.

I am using the SVN version of Django. Any help would be appreciated.

Regards,
James

Sergio

unread,
May 16, 2008, 7:47:14 AM5/16/08
to django...@googlegroups.com
Hello James,

You can do that following this:

http://code.djangoproject.com/wiki/CustomUploadAndFilters

It uses signals to customize the upload path.

James McGill escribió:


--
Un saludote..
--==@ Sergio @==--

Barraquito.net - http://www.barraquito.net

Mike Hjorleifsson

unread,
May 16, 2008, 2:55:56 PM5/16/08
to Django users
what happens in your example if sally has a holiday.rtf ?
Reply all
Reply to author
Forward
0 new messages