One further option is to run the code as a script via the command line:

python web2py.py -S myapp -M -R myscript.py

That will run myscript.py in the environment of myapp (including the 
models). This approach doesn't put code into the app that only gets 
executed one time in the life of the app.

Anthony

On Tuesday, August 4, 2015 at 5:07:15 PM UTC-4, 黄祥 wrote:
>
> another way around is i think you can define it on controllers, let say it 
> initialize.py, and put the insert db or populate in there, so when you want 
> to initialize insert db, just access the controller, after all db is 
> initalized you can either comment all the script or remove it. it seems 
> like install.php when you initialized the demo db table in most open source 
> php application.
>
> best regards,
> stifan
>
> On Wednesday, August 5, 2015 at 1:13:00 AM UTC+7, Luis Valladares wrote:
>>
>> I used a similar approach, in my appconfig.ini after pool_size i added 
>> first_run = True and wrapped every sentence i want to run only one time 
>> with if (myconf.take('db.first_run') == 'True'):
>>
>> Thanks for the help!
>>
>> El martes, 4 de agosto de 2015, 9:55:22 (UTC-4:30), Lisandro escribió:
>>>
>>> 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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to