web2py auto_import 与 Define_table
如果我们“需要访问数据但不需要访问 web2py 表属性”,我们可以使用 auto_import 的文档,但此代码似乎可以很好地使用表属性。
from gluon import DAL, Field
db = DAL('sqlite://storage.sqlite', auto_import=True)
for row in db(db.person).select():
print row.name
该表是在之前的运行中定义的。
db = DAL('sqlite://storage.sqlite', auto_import=True)
db.define_table('person',
Field('name'))
db.person[0] = {'name' : 'dave'}
db.commit()
同时执行 auto_import=True 和 Define_table 会给出有关“无效表名称”的错误。如果我尝试访问 db.table,则两者都不给出错误。
The documentation we can use auto_import if we "need access to the data but not to he web2py table attributes", but this code seems to use the table attributes just fine.
from gluon import DAL, Field
db = DAL('sqlite://storage.sqlite', auto_import=True)
for row in db(db.person).select():
print row.name
The table was defined in a previous run.
db = DAL('sqlite://storage.sqlite', auto_import=True)
db.define_table('person',
Field('name'))
db.person[0] = {'name' : 'dave'}
db.commit()
Doing both auto_import=True and the define_table gives an error about "invalid table name". Doing neither gives an error if I try to access db.table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 auto_import=True 时,web2py 将直接从应用程序“databases”文件夹中的 *.table 文件获取字段名称和类型。当文档提到不可用的“web2py 表属性”时,它指的是模型中定义的属性(即使用
db.define_table()
)但未存储在数据库中或 *.table 文件,例如“requires”、“widget”、“represent”等。这些属性仅在 web2py 代码中定义,因此不能仅通过读取 *.table 文件来确定。请注意,*.table 文件用于数据库迁移,因此它们仅存储与数据库直接相关的元数据(即字段名称和类型以及数据库级约束,例如“notnull”和“unique”)。 “requires”和“represent”等属性仅由 web2py 使用,对数据库没有影响,因此不会记录在 *.table 文件中。With
auto_import=True
, web2py will get the field names and types directly from the *.table files in the application's "databases" folder. When the documentation refers to "web2py table attributes" that will not be available, it is referring to attributes that are defined in the model (i.e., usingdb.define_table()
) but not stored in the database or *.table files, such as "requires", "widget", "represent", etc. Those attributes are defined only in web2py code and therefore cannot be determined merely by reading the *.table files. Note, the *.table files are used for database migrations, so they only store metadata directly relevant to the database (i.e., field names and types, and database-level contraints, such as "notnull" and "unique"). Attributes like "requires" and "represent" are only used by web2py and have no effect on the database, so are not recorded in the *.table files.