How to pass the db object created in controllers. to models.py

58 views
Skip to first unread message

Karl Neumann

unread,
Aug 7, 2025, 9:55:05 AMAug 7
to py4web
any import in file 'models.py' like:

from .common import db, Field

results in 404 NOT FOUND, also if there are no other programm lines in models.py.

How to pass the db-object to models.py

Head of controllers:

from yatl.helpers import A
from py4web import URL, DAL, Session, action, request
from py4web.utils.auth import Auth
from . import settings_private as s # alle App spezifische Konstanten s = settings
from . import mylogger, tools, dbaccess, models

session = Session(secret='EinradBeimRVLank')
db = DAL('sqlite://rvlank.db', folder=s.DB_FOLDER, pool_size=1)
auth = Auth(session, db, URL)
auth.enable() 

@action("index")
@action.uses("default/index.html", db, session, auth)
def index():
    return dict()

Since 2012 I use web2py for my company website, now I like to switch over to py4web.

chriii...@gmail.com

unread,
Aug 7, 2025, 11:51:35 AMAug 7
to py4web
Hi, 

Your example is a bit odd.

Why do you have the auth object in the controller? It should be in common.py. If you're new to py4web, I'd recommend creating an app using the "_scaffold" option. It'll create a well-organized structure for the new app.

In this way, your app will work out of the box, and the errors you've reported will disappear: 
"any import in file 'models.py' like: from .common import db, Field results in a 404 NOT FOUND, also if there are no other program lines in models.py."

I'm not sure if it's possible to pass db objects to models from within the controller, because models.py is used for the database tables declaration. 

If you provide more background about what you're trying to do, it'll be easier for others to help you achieve what you want.

Greetings.

Massimo DiPierro

unread,
Aug 7, 2025, 11:56:42 AMAug 7
to chriii...@gmail.com, py4web
While the code organization is unusual, it is ok to define db in controllers.py. I suspect the problem is that __init__.py is not importing your controllers.py. 

A py4web app is just a python module. Py4web imports the module (__init__.py) and it must import everything else without cycles.

--
You received this message because you are subscribed to the Google Groups "py4web" group.
To unsubscribe from this group and stop receiving emails from it, send an email to py4web+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/py4web/83147773-83f2-45b3-b351-7ac46efd29b2n%40googlegroups.com.

Karl Neumann

unread,
Aug 8, 2025, 9:19:59 AMAug 8
to py4web
thank's for your help, by reading the py4web doc the first time, I havn't really understood the function of common.py. 

Now I understood the py4web concept much better - is solved my problem.

Greetings

Massimo DiPierro

unread,
Aug 8, 2025, 9:31:05 AMAug 8
to Karl Neumann, py4web
Let us know if there is something that could be explained better.

Message has been deleted

laundmo

unread,
Aug 13, 2025, 4:27:29 PMAug 13
to py4web
personally, i've put my entire models.py contents into a big function like

def define_tables(db: DAL):
    db.define_table(...)

and i call that from common.py. Doing it this way allows me to control when things are executed through normal control flow, instead of relying on import order.

it also allows me to have logic in common.py after the define_tables(db) call which relies on those tables

Massimo DiPierro

unread,
Aug 13, 2025, 5:21:47 PMAug 13
to laundmo, py4web
I like this. Just have to make sure things are not called twice. 

Reply all
Reply to author
Forward
0 new messages