importing models into modules

31 views
Skip to first unread message

LightOfMooN

unread,
Jan 15, 2017, 8:11:54 AM1/15/17
to web...@googlegroups.com
Hello!

How can I import some function from models to my module?

for example:

mymodel.py:
def func1():
    return 'qwe'


how to call this function from
mymodule.py?

I found just one solution:
in mymodule.py:
from gluon import current
current.globalenv['func1']()

It works, but I'm not sure that it's the right thing to do.

Anthony

unread,
Jan 15, 2017, 12:50:37 PM1/15/17
to web2py-users
In your model you can do:

from gluon import current
from gluon.storage import Storage
current
.model_functions = Storage(func1=func1)

and then access current.model_functions.func1 in the module.

But a better approach would probably be to define the shared functions in a module somewhere, and then import the functions in both your model files and your other modules.

Anthony
Reply all
Reply to author
Forward
0 new messages