Brad Bollenbach wrote:
Le dimanche, 30 nov 2003, à 22:37 Canada/Eastern, Scott LeFevre a écrit :

I'm new to Python and have been frustrated by a problem specific to SQLObject.  I want to create my classes in seperate files/modules and import them into the application that I'm writing.  If I do this and try to use any of the SQLObject based classes that I've created, I always end up with an AttributeError

For example, if I try to create a new object with the new method, I'll get something like.
Traceback (most recent call last):
  File "test01.py", line 13, in ?
    r = ProcReport.new()
  File "/usr/lib/python2.2/site-packages/SQLObject/SQLObject.py", line 883, in new
    inst._SO_finishCreate(id)
  File "/usr/lib/python2.2/site-packages/SQLObject/SQLObject.py", line 904, in _SO_finishCreate
    id = self._connection.queryInsertID(self._table, self._idName,
AttributeError: 'NoneType' object has no attribute 'queryInsertID'

Now if I copy the class code in the same file as the applicate code everything works.  I've tried importing my class files via 'import classfile' and 'from classfile import *' without any success.

I realize this is a name space issue, but I thought that the 'from ... import' statement would resolve this.  Can anyone shed some light on this for me?  Can I have my classes in a seperate file or do I have to put everything in a single file for SQLObject?

It's not a namespace issue. python is telling you that a NoneType object doesn't have a method called queryInsertID, and if you notice in the error message, the object on which that method is called is self._connection.

In other words, your connection is a NoneType, which it obviously shouldn't be.

In other words, you didn't define __connection__ (or a per class _connection) in your module full of SQLObject-derived classes. One of the most recent posts to this list referred to precisely the same issue (with precisely the same error message.)

Thank you for your help.  Since I'm new to Python, I don't have a good grasp of how global variables work and how to use them properly.  I've tried the following:

Created a ProcReport class based on SQLObject and saved it in a seperate file.  I then imported this file into my test app.  Please see below.

_ProcReport.py
---------------------------------------------------
from SQLObject import *

class ProcReport(SQLObject):

        #db columns/fields
        sourceorg = StringCol(length=10, default=None)
        mrn = StringCol(length=10, default=None)
        lastname = StringCol(length=10, default=None)
        firstname = StringCol(length=10, default=None)
        middlename = StringCol(length=10, default=None)
        dateofbirth = DateTimeCol(default=None)
        gender = StringCol(length=1, default=None)
        orderingmd = StringCol(length=20, default=None)
        procdesc = StringCol(length=20, default=None)
        srvdatetime = DateTimeCol(default=None)
        readingmd = StringCol(length=20, default=None)
        reporttext = StringCol(default=None)
        reporthtml = StringCol(default=None)
        reportsource = StringCol(default=None)
-------------------------------------


test01.py
-----------------------------------
from SQLObject import *
 __connection__  = MySQLConnection(host='localhost', user='xxxxx', passwd='zzzz', db='ReportDR')

from _ProcReport import ProcReport


ProcReport.createTable()
---------------------------------

When I run the test01.py app I get the AttributeError: 'NoneType'  as I listed in a previous post.  My best guess is the __connection__ variable isn't available when the class is declared.  I've tested this by inserting the following code into both files:

for k,v in globals().items():
        print k,'=',v

the __connection__ variable doesn't print from the _ProcReport file but does from the test01 file.

I hope this is clear. I'm stumped as to why this doesn't work.  Could someone please share with me a clean way of accomplishing this?  Or, a good example of how to use global variables?

Thank you for you help!!

--
Scott LeFevre, Consultant
Solutio Informatio