standalone use of DAL in web2py

左心房为你撑大大i 提交于 2019-12-01 23:43:31

You can use pydal

pip install pydal

and then:

from pydal import DAL, Field
...

Your code refers to db.user, but you have not defined a "user" table in your code. You cannot import the table definitions from an app model file, as it is not a Python module. One solution might be the following:

db = DAL('sqlite://' + db_name, folder=dbpath, auto_import=True)

That will read the table metadata from the *.table files and create table definitions, though the definitions will not includes some of the web2py-specific model attributes, such as field validators, labels, etc. If you need full DAL table definitions with all the field attributes, you will have to include them directly in your external code. For more details, see here.

Also, do import gluon.dal, not gluon.sql, which was deprecated a long time ago (it simply refers to gluon.dal).

You have to install pydal seperately from the pydal/dal version you receive from the web2py enviroment. (Also check the pydal version in your copy of web2py and install the same version into python)

pip install pydal==some_version_number

Note: You have to use an explicit path to the folder where the db tables are located.

from pydal import DAL

db = DAL("some_db_uri", folder="/some/full_path_to/database", auto_import=True)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!