468,115 Members | 2,050 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,115 developers. It's quick & easy.

Type error when using SQLAlchemy and Python

HI,

I posted this to sqlalchemy but didnt get a response, so I was
wondering if anyone on python-list could help me.

I m currently trying to build an api for a database and I really like
the way that Djangos manager ( Class_name.objects ) is set up. This
seems very intuitive for me. After reading through the some of the
django source and getting slightly confused I ve implemented a basic
version for one of my tables. The problem is I get an exception, when
I m not expecting one.

registry.py --contains table definitions etc.

Manager.py

from Registry import *

class Manager(object):
def __init__(self, model, table=None):
self.model = model
self.table = table
def get(self, slice):
pass
def all(self):
print "1"
mapper(self.model, interpro_table)
print "2"
session = create_session()
print "3"
query = session.query(self.model)
print "4"
return query.select()

Models.py

from Manager import *

class ModelBase(type):
def __new__(cls, name, bases, dict):
print cls
setattr(cls, 'objects', Manager(cls))
return type.__new__(cls, name, bases, dict)

class Model(object):
__metaclass__=ModelBase

class InterPro(Model):
_tableName = interpro_table
def __init__(self, interpro_ac):
self.interpro_ac = interpro_ac
def __str__(self):
return "InterPro: %s" %(self.interpro_ac)
def __repr__(self):
return "InterPro: %s" %(self.interpro_ac)

if __name__=='__main__':
a = Manager(InterPro)
print a
print a.all() --this prints out all of the objects in the database
i = InterPro('IPR014697')
print InterPro.objects
print InterPro.objects.all()
--this fails and produces the exception.

Traceback (most recent call last):
File "Model.py ", line 28, in ?
print InterPro.objects.all()
File "/home/skeg/workspace/test/src/Manager.py", line 17, in all
return query.select()
File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/query.py",
line 247, in select
return self.select_whereclause(whereclause=arg, **kwargs)
File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/query.py",
line 252, in select_whereclause
return self._select_statement(statement, params=params)
File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/query.py",
line 378, in _select_statement
return self.execute(statement, params=params, **kwargs)
File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/query.py",
line 310, in execute
return self.instances(result, **kwargs)
File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/query.py",
line 329, in instances
self.mapper._instance(context, row, result)
File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/mapper.py",
line 1213, in _instance
instance = self._create_instance(context.session)
File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/mapper.py",
line 1234, in _create_instance
obj = self.class_.__new__(self.class_)
TypeError: __new__() takes exactly 4 arguments (1 given)

Does anyone know what the problem is? Is it the way I ve programmed
this using metaclasses or is it sqlalchemy and the way in instantiates
objects or even both?

Many Thanks in advance,
Jun 11 '07 #1
0 1202

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

108 posts views Thread by Bryan Olson | last post: by
20 posts views Thread by John Salerno | last post: by
2 posts views Thread by George Sakkis | last post: by
6 posts views Thread by Ilias Lazaridis | last post: by
11 posts views Thread by Frederic Rentsch | last post: by
3 posts views Thread by Greg Copeland | last post: by
2 posts views Thread by Adam Kubica | last post: by
2 posts views Thread by hwcowan | last post: by
5 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.