我正在尝试动态构建名为
我的问题是,每当我在浏览器上刷新并查看
我在
某种情况下,
db.blog
和db.code
的表,它们具有完全相同的SQL定义。在定义它们之后,我希望用10行随机数据填充它们,并且永远不执行该初始化代码。我的问题是,每当我在浏览器上刷新并查看
db.code
或db.blog
的newblog appadmin界面时,初始化代码就会执行一次:https://172.25.1.1/newblog/appadmin/select/db?query=db.code.id>0我在
newblog/models/newblog.py
中初始化了db.blog
和db.code
:from gluon import *
from gluon.contrib.populate import populate
## initialize db.blog and db.code:
## At runtime, build TAGGED_TABLES (once)
TAGGED_TABLES = set(['blog', 'code'])
for tt in TAGGED_TABLES:
if not db.get(tt, False):
db.define_table(tt,
Field('name', length=32, notnull=True),
Field('value', length=65535, notnull=True),
Field('tags', type='list:reference tag', unique=False, notnull=False),
)
populate(db.get(tt), 10)
## cross-reference db.tagged_tables to this one
db.tagged_tables.insert(name=tt,
database_pointer='reference %s' % tt)
db.commit()
某种情况下,
if not db.get(tt, False):
允许下面的例程被多次执行。我不明白为什么...如果表已经被创建了,那么 not db.get(tt, False)
应该是 False
。 然而,web2py 从不跳过初始化代码,这意味着每次重新加载时 db.blog
和 db.code
都会增加 10 个条目。
问题: 为什么 if not db.get(tt, False):
没有阻止多次执行?
我在 Debian 6.0 / sqlite 3.7.3 / Cherokee 1.2.101 / uWSGI 0.9.9.3 上运行 web2py 1.99.4。
解决方案
根据 Interrobang 的答案,编写正确的方式如下:
from gluon import *
from gluon.contrib.populate import populate
TAGGED_TABLES = set(['blog', 'code'])
for tt in TAGGED_TABLES:
# db.define_table() must be called on **every page**
# this sets things up in memory...
db.define_table(tt,
Field('name', length=32, notnull=True),
Field('value', length=65535, notnull=True),
Field('tags', type='list:reference tag', unique=False, notnull=False),
)
## initialize db.blog and db.code:
## At runtime, populate tables named in TAGGED_TABLES (once)
if not (db(db.get(tt).id>0).select()):
populate(db.get(tt), 10)
## cross-reference db.tagged_tables to this table (named in var tt)
db.tagged_tables.insert(name=tt,
database_pointer='reference %s' % tt)
db.commit()
现在db.blog
和db.code
保持恒定大小。
摘要
必须为每个页面呈现调用db.define_tables()
;我的理解是错误的(只需要运行一次将表定义写入磁盘)。