Model.py and importing

4 views
Skip to first unread message

percious

unread,
May 18, 2006, 1:28:49 PM5/18/06
to TurboGears
For my own set of very good reasons, I want to have all of my
SQLObjects in a separate file for some other processes that I do away
from TG. I attempted to do this by importing my module to Models.py
with the following line of code:

from ft8.turboLink.help.HelpDatabaseModel import *

Catwalk can read the fact that there are tables in the SQL database
corresponding to the objects, but it cannot read the values in the
table. Instead, it spits out the following error:

2006-05-18 13:27:56,956 turbogears.identity INFO Identity is
available...
2006-05-18 13:27:56,971 cherrypy.msg INFO HTTP: Page handler: <bound
method Brow
se.index of <turbogears.toolbox.catwalk.browse.Browse object at
0x012F3AD0>>
Traceback (most recent call last):
File
"c:\python24\lib\site-packages\cherrypy-2.2.1-py2.4.egg\cherrypy\_cphttpt
ools.py", line 105, in _run
self.main()
File
"c:\python24\lib\site-packages\cherrypy-2.2.1-py2.4.egg\cherrypy\_cphttpt
ools.py", line 254, in main
body = page_handler(*virtual_path, **self.params)
File "<string>", line 3, in index
File
"c:\python24\lib\site-packages\TurboGears-0.9a6-py2.4.egg\turbogears\cont
rollers.py", line 273, in expose
output = database.run_with_transaction(
File
"c:\python24\lib\site-packages\TurboGears-0.9a6-py2.4.egg\turbogears\data
base.py", line 221, in run_with_transaction
retval = func(*args, **kw)
File "<string>", line 5, in _expose
File
"c:\python24\lib\site-packages\TurboGears-0.9a6-py2.4.egg\turbogears\cont
rollers.py", line 290, in <lambda>
mapping, fragment, *args, **kw)))
File
"c:\python24\lib\site-packages\TurboGears-0.9a6-py2.4.egg\turbogears\cont
rollers.py", line 314, in _execute_func
output = errorhandling.try_call(func, *args, **kw)
File
"c:\python24\lib\site-packages\TurboGears-0.9a6-py2.4.egg\turbogears\erro
rhandling.py", line 71, in try_call
return func(self, *args, **kw)
File
"c:\python24\lib\site-packages\TurboGears-0.9a6-py2.4.egg\turbogears\tool
box\catwalk\browse.py", line 24, in index
total,rows =
self.rows_for_model(object_name,start,page_size,filters)
File
"c:\python24\lib\site-packages\TurboGears-0.9a6-py2.4.egg\turbogears\tool
box\catwalk\browse.py", line 115, in rows_for_model
total = query.count()
File
"c:\python24\lib\site-packages\sqlobject-0.7.1dev_r1588-py2.4.egg\sqlobje
ct\sresults.py", line 188, in count
count = self.accumulate('COUNT(*)')
File
"c:\python24\lib\site-packages\sqlobject-0.7.1dev_r1588-py2.4.egg\sqlobje
ct\sresults.py", line 171, in accumulate
conn = self._getConnection()
File
"c:\python24\lib\site-packages\sqlobject-0.7.1dev_r1588-py2.4.egg\sqlobje
ct\sresults.py", line 38, in _getConnection
return self.ops.get('connection') or self.sourceClass._connection
File
"c:\python24\lib\site-packages\sqlobject-0.7.1dev_r1588-py2.4.egg\sqlobje
ct\dbconnection.py", line 881, in __get__
return self.getConnection()
File
"c:\python24\lib\site-packages\sqlobject-0.7.1dev_r1588-py2.4.egg\sqlobje
ct\dbconnection.py", line 893, in getConnection
raise AttributeError(
AttributeError: No connection has been defined for this thread or
process
Request Headers:
COOKIE: tg-visit=28a0c3d3b8caa54d4db95cae37f7cd36972f4c66
Content-Length:
ACCEPT-CHARSET: ISO-8859-1,utf-8;q=0.7,*;q=0.7
USER-AGENT: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
rv:1.8.0.3) Gecko/
20060426 Firefox/1.5.0.3
CONNECTION: keep-alive
HOST: localhost:8081
ACCEPT:
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/pl
ain;q=0.8,image/png,*/*;q=0.5
Remote-Addr: 127.0.0.1
ACCEPT-LANGUAGE: en-us,en;q=0.5
Content-Type:
Remote-Host: 127.0.0.1
ACCEPT-ENCODING: gzip,deflate
KEEP-ALIVE: 300
127.0.0.1 - - "GET /catwalk/browse/?object_name=CitectServer HTTP/1.1"
500 3356
"" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3)
Gecko/20060426 F
irefox/1.5.0.3"

If I copy and paste my objects into Model.py I get my desired results,
but we all know what kind of drudgery copy and paste can get us into.

Anyone have any ideas?

-chris

swordsp

unread,
May 19, 2006, 12:23:17 AM5/19/06
to turbo...@googlegroups.com
Hi,

You need to set the _connection attribute for each model object, so there are two solutions as I know:

1. Add code like this to each module include model object, sqlobject will do it automatically:

from turbogears.database import PackageHub
hub = PackageHub("somepackage")
__connection__ = hub

2. If you don't want to modify the imported module, then set it manually for each imported object in model.py :

MyModel._connection = __connection__

It works, but seems not as easy as it should be.
I also want to know if there are any better solution.

"c:\python24\lib\site-packages\TurboGears- 0.9a6-py2.4.egg\turbogears\data

base.py", line 221, in run_with_transaction
    retval = func(*args, **kw)
  File "<string>", line 5, in _expose
  File
"c:\python24\lib\site-packages\TurboGears- 0.9a6-py2.4.egg\turbogears\cont

rollers.py", line 290, in <lambda>
    mapping, fragment, *args, **kw)))
  File
"c:\python24\lib\site-packages\TurboGears-0.9a6-py2.4.egg\turbogears\cont
rollers.py ", line 314, in _execute_func
    output = errorhandling.try_call(func, *args, **kw)
  File
"c:\python24\lib\site-packages\TurboGears-0.9a6-py2.4.egg\turbogears\erro
rhandling.py", line 71, in try_call
    return func(self, *args, **kw)
  File
"c:\python24\lib\site-packages\TurboGears-0.9a6-py2.4.egg\turbogears\tool
box\catwalk\browse.py", line 24, in index
    total,rows =
self.rows_for_model (object_name,start,page_size,filters)
  File
"c:\python24\lib\site-packages\TurboGears-0.9a6-py2.4.egg\turbogears\tool
box\catwalk\browse.py", line 115, in rows_for_model
    total = query.count()
  File
"c:\python24\lib\site-packages\sqlobject-0.7.1dev_r1588-py2.4.egg\sqlobje
ct\sresults.py", line 188, in count
    count = self.accumulate('COUNT(*)')
  File
"c:\python24\lib\site-packages\sqlobject- 0.7.1dev_r1588-py2.4.egg\sqlobje

percious

unread,
May 25, 2006, 2:58:54 PM5/25/06
to TurboGears
Ok, so I *sorta* took your advice, but came up with a more *generic*
solution:


import ft8.turboLink.help.HelpDatabaseModel as HelpDatabaseModel

classes=[]
for value in dir(HelpDatabaseModel):
#print value
try:
#print "HelpDatabaseModel.%s"%value
if issubclass(eval("HelpDatabaseModel.%s"%value), SQLObject):
if value!='SQLObject':
classes.append(value)
except:
pass

del(HelpDatabaseModel)

from ft8.turboLink.help.HelpDatabaseModel import *

for c in classes:
exec("%s._connection = __connection__"%c)

A true hack, complete with the usual execs and evals. However,
I can update my helpdatabasemodel file to my hearts content and never
have to touch model.py.

Also, I can USE helpdatabasemodel in other code without it croaking.
Win Win? Eh?

What does everyone think?

-percious

Sean Jamieson

unread,
May 26, 2006, 11:55:34 AM5/26/06
to TurboGears
Why are you using eval anyway? You don't need it here.

if issubclass( getattr( HelpDatabaseModel, value ), SQLObject ):
... and ...
for c in classes:
gatattr( HelpDatabaseModel, c ). _connection = __connection__

Sean

percious

unread,
May 26, 2006, 1:15:49 PM5/26/06
to TurboGears
Hmm. That first part works, but because im including * from
helpdatabasemodel the second part does not.

I took what you wrote, and came up with this:

import ft8.turboLink.help.HelpDatabaseModel as HelpDatabaseModel

classes=[]
for value in dir(HelpDatabaseModel):

try:
c = getattr( HelpDatabaseModel, value)
if issubclass( c, SQLObject ):
if value!='SQLObject':
classes.append(c)
except:
pass

del(HelpDatabaseModel)

from ft8.turboLink.help.HelpDatabaseModel import *

for c in classes:
c._connection = __connection__

ah. no more evals. Thanks for the help! Much less Hacky now.

-percious

Reply all
Reply to author
Forward
0 new messages