If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!
Package type:




Since web2py definition table is done in the models and models are executed in each request, some times tables will be defines even if you do no need it.

I have spent a few hours looking for a solution in oder to  minimize the code in models and work easily when they have a dependencies. In this case the table definition. This solution is inspired in Movuca and http://www.slideshare.net/martinpm/web2py-pensando-en-grande-9448110 presentation.

In this small app you can see how the models have been defined in a class into the module folder and only load or define the tables you need for each request.

The example below will define the table “comments” and “post”. This is done because there is a dependency between them.

from model import DataBase
DataBase(db=self.db, auth=self.auth, request=self.request, tables=['t_comments'])

The example below, only will define the table “docs”:

from model import DataBase
DataBase(db=self.db, auth=self.auth, request=self.request, tables=['t_docs'])

If you do not specify any table, by default the module will define all. This is good for the first request of the app or even if you want to populate the entire database.

DataBase(db=self.db, auth=self.auth, request=self.request) 


Example of the app where you test a few options:


Example loading the "t_docs" table content. In the terminal you can see only "t_docs" is loaded. "t_comments" and "t_post" tables are ignored for this request.




Related slices

Comments (1)

Hosting graciously provided by:
Python Anywhere