upload_to vs FileSystemStorage

620 views
Skip to first unread message

Dave

unread,
Sep 8, 2010, 8:04:28 AM9/8/10
to Django users
Hi everyone,

I've found in documentation usage of custom file storage. There it is
like that ( http://docs.djangoproject.com/en/1.2/topics/files/#the-built-in-filesystem-storage-class
) -

from django.db import models
from django.core.files.storage import FileSystemStorage

fs = FileSystemStorage(location='/media/photos')

class Car(models.Model):
...
photo = models.ImageField(storage=fs)

Notice there is no upload_to attribute in ImageField. I suppose, it's
quite obvious, because I will set it by attribute location in
initializing FileSystemStorage object fs. But if I'll do it this way
it'll rise an error - Error: One or more models did not validate:
app.mymodel: "image": FileFields require an "upload_to" attribute.

What am I missing? Or is it possiblle to have upload_to somehow set to
an empty string? So the location would be the same as it is declared
in the file storage object initialization.

Preston Holmes

unread,
Sep 9, 2010, 9:29:02 AM9/9/10
to Django users


On Sep 8, 5:04 am, Dave <kru...@seznam.cz> wrote:
> Hi everyone,
>
> I've found in documentation usage of custom file storage. There it is
> like that (http://docs.djangoproject.com/en/1.2/topics/files/#the-built-in-files...
> ) -
>
> from django.db import models
> from django.core.files.storage import FileSystemStorage
>
> fs = FileSystemStorage(location='/media/photos')
>
> class Car(models.Model):
>     ...
>     photo = models.ImageField(storage=fs)
>
> Notice there is no upload_to attribute in ImageField. I suppose, it's
> quite obvious, because I will set it by attribute location in
> initializing FileSystemStorage object fs. But if I'll do it this way
> it'll rise an error - Error: One or more models did not validate:
> app.mymodel: "image": FileFields require an "upload_to" attribute.
>
> What am I missing? Or is it possiblle to have upload_to somehow set to
> an empty string? So the location would be the same as it is declared
> in the file storage object initialization.

You've basically answered your question - if you read the docs for
FileField you will see upload_to is a required field. It defines path
relative to storage root. so empty string or '/' should do it.

-Preston

Stodge

unread,
Oct 4, 2010, 10:48:56 AM10/4/10
to Django users
If I do upload_to=os.sep, I get an error:

Attempted access to '/test.pdf' denied.

It's trying to upload to the root of the drive!

settings.DOCUMENT_STORE_PATH is something like /var/www/testproject/
documents

file =
models.FileField(storage=FileSystemStorage(location=settings.DOCUMENT_STORE_PATH),
upload_to=os.sep, help_text=_(u"""The file itself."""))

Shouldn't it append the location and the upload_to paths to get the
target destination?

On Sep 9, 9:29 am, Preston Holmes <pres...@ptone.com> wrote:
> On Sep 8, 5:04 am, Dave <kru...@seznam.cz> wrote:
>
>
>
>
>
> > Hi everyone,
>
> > I've found in documentation usage of custom file storage. There it is
> > like that (http://docs.djangoproject.com/en/1.2/topics/files/#the-built-in-files...
> > ) -
>
> > from django.db import models
> > from django.core.files.storage import FileSystemStorage
>
> > fs = FileSystemStorage(location='/media/photos')
>
> > class Car(models.Model):
> >     ...
> >     photo = models.ImageField(storage=fs)
>
> > Notice there is noupload_toattribute in ImageField. I suppose, it's
> > quite obvious, because I will set it by attribute location in
> > initializing FileSystemStorage object fs. But if I'll do it this way
> > it'll rise an error - Error: One or more models did not validate:
> > app.mymodel: "image": FileFields require an "upload_to" attribute.
>
> > What am I missing? Or is it possiblle to haveupload_tosomehow set to
> > an empty string? So the location would be the same as it is declared
> > in the file storage object initialization.
>
> You've basically answered your question - if you read the docs for
> FileField you will seeupload_tois a required field.  It defines path
Reply all
Reply to author
Forward
0 new messages