I am trying to dynamically create tables called db.blog and db.code with exactly the same SQL definitions. After I define them, I want to fill them with 10 lines of random data and never run this initialization code again.
My problem is that the initialization code is executed every time I click update in the browser, when I look at the appadmin newblog interface for db.code or db.blog : https://172.25.1.1/newblog/appadmin/select/db ? query = db.code.id > 0
I initialize db.blog and db.code in newblog/models/newblog.py :
from gluon import * from gluon.contrib.populate import populate
Somehow if not db.get(tt, False): allows you to perform several subroutine executions under it. I do not understand why ... if the table has already been created, then not db.get(tt, False) should be False . However, web2py never misses the initialization code, which means that db.blog and db.code grow by 10 entries with every reboot.
Question: Why not if not db.get(tt, False): preventing multiple executions?
I am running web2py 1.99.4 on Debian 6.0 / sqlite 3.7.3 / Cherokee 1.2.101 / uWSGI 0.9.9.3
Decision
Based on Interrobang answer the correct way to write:
from gluon import * from gluon.contrib.populate import populate TAGGED_TABLES = set(['blog', 'code']) for tt in TAGGED_TABLES:
Now db.blog and db.code remain constant.
Summary
db.define_tables() should be called for each page rendering; my understanding (that he only needed to write the table definition to disk once) was incorrect.
python database web2py
Mike pennington
source share