'DAL" object has no attribute 'colors'

55 views
Skip to first unread message

Richard Gordon

unread,
Aug 17, 2022, 1:44:02 AM8/17/22
to py4web
I'm trying to access a table and field from controllers.py. The table and field are defined in models.py.

The controller is defined as follows:

from py4web import action, request, abort, redirect, URL
from py4web import DAL
from .common import db, session, T, cache, auth, logger, authenticated, unauthenticated, flash
@action('put')
@action.uses(db)
def put():
        color=request.query.get('color') # this works fine
        color_id=db(db.colors.code==color).select().first().id

When I put in the correct url in my browser I get the following error message at the console:

'DAL' object has no attribute 'colors'

Any thoughts about what's causing the problem?

Massimo DiPierro

unread,
Aug 17, 2022, 1:49:18 AM8/17/22
to Richard Gordon, py4web
try print db.tables in the action  maybe you have two files which both define a db?

--
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 on the web visit https://groups.google.com/d/msgid/py4web/e94ba576-fa10-4b2d-bd7e-0daa565ea87an%40googlegroups.com.

Richard Gordon

unread,
Aug 17, 2022, 1:55:25 AM8/17/22
to py4web
print(db.tables) produces output []

Richard Gordon

unread,
Aug 17, 2022, 2:45:31 AM8/17/22
to py4web
I'm also using the scaffold __init__.py
--------
# check compatibility
import py4web

assert py4web.check_compatible("0.1.20190709.1")

# by importing db you expose it to the _dashboard/dbadmin
from .models import db

# by importing controllers you expose the actions defined in it
from . import controllers
-----

and the sqlite database is getting created and loaded correctly. I can see my data using the dashboard.

Richard Gordon

unread,
Aug 17, 2022, 2:47:48 AM8/17/22
to py4web
I will look around and see if I can find if the db is being defined a second time somewhere.

Richard Gordon

unread,
Aug 17, 2022, 2:56:07 AM8/17/22
to py4web
So I looked at all the files in my app and there is only one place, in models.py, where db is defined:

db=DAL('sqlite://storage.sqlite', folder=DB_FOLDER)

and DB_FOLDER is consistent throughout.

Richard Gordon

unread,
Aug 17, 2022, 3:23:34 AM8/17/22
to py4web
I dug deeper and did find another 

db=DAL('sqlite://storage.sqlite', folder=DB_FOLDER)

in my controller.py.

I commented it out and now,  db.tables contains: ['auth_user', 'auth_user_tag_groups']

Richard Gordon

unread,
Aug 17, 2022, 3:26:45 AM8/17/22
to py4web
However, still no joy. Still getting error message:  'DAL' object has no attribute 'colors'

Richard Gordon

unread,
Aug 17, 2022, 4:05:34 AM8/17/22
to py4web
I renamed my db to a new name (not db) and all my tables are now visible. It looks like db is being set somewhere other than my models.py file, but I can't find it.

Jim Steil

unread,
Aug 17, 2022, 11:17:15 AM8/17/22
to py4web
Can you share the contents of models.py, common.py and controllers.py?

-Jim

Richard Gordon

unread,
Aug 17, 2022, 11:38:52 AM8/17/22
to py4web
OK. I think I found the second occurrence of db and all is well now.

I'm liking py4web. Much more modular, which is ideal for my application. Trivial transition from web2py (given my extensive web2py background).

Jim Steil

unread,
Aug 17, 2022, 11:51:14 AM8/17/22
to py4web
Agreed - py4web has a lot more to offer.

Let us know if you have any further questions.

-Jim
Reply all
Reply to author
Forward
0 new messages