> You can use the DAL from modules as well. As Niphlod suggested, you can 
>> also use the scheduler to schedule and run a task in the context of another 
>> app, though that might not be as fast as you'd like, as even setting 
>> immediate=True, it could take up to "heartbeat" seconds for the worker to 
>> pick up and execute the task, and then you have to check for the results.
>>
>>
> I can use the DAL, but can I use the "db" object defined in my models 
> file?  Or do you mean I could use the DAL if I rewrote my other app to use 
> a "model-less" approach where the models were defined in some other manner?
>

Yes, the point was that you can put whatever you want into modules, which 
means the code can be shared across apps. If you want to share the code to 
create a database connection object, then put that code in a module. Here 
is the basic idea:

/app1/modules/db.py:

import os
from gluon import *

def dal(**kwargs):
    web2py_folder = current.request.env.applications_parent
    db_folder = os.path.abspath(os.path.join(web2py_folder, 'applications',
                                             'app1', 'databases'))
    return DAL('sqlite://storage.sqlite', folder=db_folder, **kwargs)

def define_tables(db):
    db.define_table('table1', Field('field1'))

/app2/models/db.py:

from applications.app1.modules.db import dal, define_tables

app1_db = dal()
define_tables(app1_db)

# Now use app1_db as usual.

Of course, the module defining the database connection and tables doesn't 
have to be in the /modules folder of any particular application, nor does 
the folder specified as the "folder" argument -- that can all be 
centralized somewhere if desired.

Also, note there is a way to share models defined in the model files of 
another app, though with limitations. See 
http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Using-DAL-without-define-tables.
 
That approach can be used to access model definitions from app1 model files 
from within app2 model files. However, the model definitions within app2 
will only include the table and field attributes that can be gleaned from 
the *.table migration files in app1's /databases folder, which only 
includes attributes needed for the database schema definition (i.e., it 
doesn't include any web2py-specific attributes, such as validators, labels, 
represent functions, etc.).

Anthony

-- 
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