The way I do it is this: I define a module and I define in there a function 
that does db initialization. 

modules/myfunctions.py
# -*- coding: utf-8 -*-

from gluon import *

def initialize_db():
    db = current.db
    # here, do all needed inserts to database tables


Then, in db.py (after table definitions):

FIRST_RUN = False

if FIRST_RUN:
    from myfunctions import initialize_db
    initialize_db()


So only thing  you need to do is set FIRST_RUN = True, run the website one 
time, and then set it back to False.
A better approach would be reading FIRST_RUN from a configuration file, an 
ini file or something like that.


Of course you could do it directly in db.py, for example, checking the 
total count of records of some table you know how many records should have 
(for example, auth_group table), but that's not good for performance. 


El martes, 4 de agosto de 2015, 11:08:04 (UTC-3), Luis Valladares escribió:
>
> Hello!
>
> Im trying to populate some fields inside my database each time is created, 
> things like creating some groups in auth_groups, creating triggers for all 
> the tables and adding some records in other tables, but i want this to be 
> done ONLY when web2py creates the database because a lot of this records 
> must be unique, so i created a file inside models to add all this info, but 
> it get executed on every request of the site, i managed tu handle this 
> using try..except Exception, but i dont think this is a good practice 
> because it stops at every error without notifying me.
>
> There is a way to set a model file, or a set of instructions to execute 
> ONLY when web2py create the tables of the database?
>
> Thanks a lot!
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to