Hi All,

I'm new to Turbo gears so if I'm missing something obvious please let
me know. I'm trying to split model.py into a models directory. The FAQ
on the subject wasn't very helpful: 
http://docs.turbogears.org/FAQ?action=show&redirect=1.0%2FFAQ#q-006

I've done:
- created models/machine.py with a bunch of classes like
from sqlobject import *

class Machine(SQLObject):
    class sqlmeta:
        fromDatabase = True
        idName = "machine_id"

- added the following to models/__init__.py
from machine import *

- changed the top level model.py to
from turbogears.database import PackageHub
from sqlobject import *

hub = PackageHub('knightly')
__connection__ = hub

from models import *

This gives me the following traceback:
Traceback (most recent call last):
  File "start-knightly.py", line 24, in ?
    from knightly.controllers import Root
  File "/home/brianc/svn_knightly/trunk/knightly/knightly/controllers/
__init__.py", line 1, in ?
    from knightly.controllers.root import Root
  File "/home/brianc/svn_knightly/trunk/knightly/knightly/controllers/
root.py", line 2, in ?
    from knightly.model import *
  File "/home/brianc/svn_knightly/trunk/knightly/knightly/model.py",
line 7, in ?
    from models import *
  File "/home/brianc/svn_knightly/trunk/knightly/knightly/models/
__init__.py", line 1, in ?
    from machine import *
  File "/home/brianc/svn_knightly/trunk/knightly/knightly/models/
machine.py", line 4, in ?
    class Machine(SQLObject):
  File "/usr/lib/python2.4/site-packages/SQLObject-0.7.7-py2.4.egg/
sqlobject/declarative.py", line 109, in __new__
    cls.__classinit__(cls, new_attrs)
  File "/usr/lib/python2.4/site-packages/SQLObject-0.7.7-py2.4.egg/
sqlobject/main.py", line 782, in __classinit__
    cls.sqlmeta.addColumnsFromDatabase()
  File "/usr/lib/python2.4/site-packages/SQLObject-0.7.7-py2.4.egg/
sqlobject/main.py", line 426, in addColumnsFromDatabase
    conn = connection or soClass._connection
  File "/usr/lib/python2.4/site-packages/SQLObject-0.7.7-py2.4.egg/
sqlobject/dbconnection.py", line 890, in __get__
    return self.getConnection()
  File "/usr/lib/python2.4/site-packages/SQLObject-0.7.7-py2.4.egg/
sqlobject/dbconnection.py", line 902, in getConnection
    raise AttributeError(
AttributeError: No connection has been defined for this thread or
process

The problem is the __connection__ declaration in model.py is not good
enough for machine.py to pick up the database connection.
hub = PackageHub('knightly')
__connection__ = hub

If I add that declaration to machine.py the connection is found.
However, this is ugly as it would require me to add that idiom at the
top of every model file (futhermore, I want to reuse the same model
definitions for the backend system that will be populating the
database; that system shouldn't even know about turbogears).

I could hack model.py with the following:
CONNECTION_STRING = "mysql://%s:[EMAIL PROTECTED]/%s" % \
                    (MYSQL_USER, MYSQL_PASS, MYSQL_HOST, MYSQL_DBNAME)

sqlhub.processConnection = connectionForURI(CONNECTION_STRING)

However, this circumvents turbo gears configuration file, which leaves
me with a dirty feeling inside. Is there another way to accomplish
this without code duplication and without declaring the connection so
explicitly?

Thanks,
Brian


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to