Change upload behaviour: standard filename

109 views
Skip to first unread message

grezly

unread,
Jan 17, 2011, 9:36:19 AM1/17/11
to web2py-users
I'd like to change the default behaviour for one app, concerning my
uploads. Web2py normally rename the uploaded file, but in my
particular case, web2py doesn't need to change my filename.

Is something possible, while creating my model?

Next to that i also like to store a hash of a file in my database.
With the standard crud i can't change this behaviour.
I also can't find such a functionality right now in web2py/crud, or
did i miss something?

Massimo Di Pierro

unread,
Jan 17, 2011, 12:09:08 PM1/17/11
to web2py-users
You can pass two optional arguments to Field(...)

custom_store and custom_retrieve

the first must be a function that takes a file like object, a
filename, and an optional path (uploadfolder) stores the file where
you like and returns the filename you assign to the file (can be the
original one)

custom_retrieve instead takes a filename and a path (uploadfolder) and
return the file content.

Massimo

Amber Doctor

unread,
Mar 5, 2013, 3:53:39 PM3/5/13
to web...@googlegroups.com
I've added the optional arguments to my field, and created the functions as described.  However I'm getting the error:

<type 'exceptions.NameError'> name 'store_file' is not defined


Do the functions need to live in a specific location in order to be recognized? 

I looked through the book but I didn't see an example of these in practice.  The example I was looking at is on stack over flow: http://stackoverflow.com/a/8185933

Thanks

Jim Gregory

unread,
Mar 5, 2013, 10:02:12 PM3/5/13
to web...@googlegroups.com
Did you include the code for the store_file and retrieve_file functions listed in the Stack Overflow answer before the table definitions in your model file?

-Jim

Amber Doctor

unread,
Mar 10, 2013, 3:49:31 PM3/10/13
to web...@googlegroups.com
Thanks, that's what I was missing.  I was thinking they needed to be in a particular controller not in the model.
Reply all
Reply to author
Forward
0 new messages