Thread: [SQLObject] Problem with RelatedJoin
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: TiNo <ti...@gm...> - 2007-05-12 17:40:14
|
Hi, I have the following model (in turbogears, but I think the problem is pure SQLObject) (only relevant parts copied): ---------------------------------------------------- class Photo(SQLObject): filename = StringCol(length=30,alternateID=True) keywords = RelatedJoin('Keyword') [etc...] class Keyword(SQLObject): keyword = StringCol(alternateID=True) photo = RelatedJoin('Photo') ------------------------------------------------------ Now I want to try the following. I want Photo to accept a string of keywords, and then add these keywords to the foto. I tried adding the following method to Photo, but it gives me an error: ------------------------------------------------------ def _set_keywords(self,value): # accepts value as a string. Multiple keywords should be comma-seperated keywords = value.split(',') for keyword in keywords: keyword = keyword.strip() try: ref = Keyword.byKeyword(keyword) except SQLObjectNotFound: ref = Keyword(keyword=keyword) self.addKeyword(ref) ----------------------------------------------------- And the traceback: ---------------------------------------------------- Page handler: <bound method Root.import_photos of <photon.controllers.Root object at 0x01559370>> Traceback (most recent call last): File "p:\python25\lib\site-packages\cherrypy-2.2.1-py2.5.egg\cherrypy \_cphttptools.py", line 105, in _run self.main() File "p:\python25\lib\site-packages\cherrypy-2.2.1-py2.5.egg\cherrypy \_cphttptools.py", line 254, in main body = page_handler(*virtual_path, **self.params) File "<string>", line 3, in import_photos File "p:\python25\lib\site-packages\TurboGears-1.0.2.2-py2.5.egg \turbogears\controllers.py", line 336, in expose *args, **kw) File "<string>", line 5, in run_with_transaction File "p:\python25\lib\site-packages\TurboGears-1.0.2.2-py2.5.egg \turbogears\database.py", line 303, in so_rwt retval = func(*args, **kw) File "<string>", line 5, in _expose File "p:\python25\lib\site-packages\TurboGears-1.0.2.2-py2.5.egg \turbogears\controllers.py", line 351, in <lambda> mapping, fragment, args, kw))) File "p:\python25\lib\site-packages\TurboGears-1.0.2.2-py2.5.egg \turbogears\controllers.py", line 378, in _execute_func output = errorhandling.try_call(func, *args, **kw) File "p:\python25\lib\site-packages\TurboGears-1.0.2.2-py2.5.egg \turbogears\errorhandling.py", line 73, in try_call return func(self, *args, **kw) File "F:\Dev\Python\Photon\photon\controllers.py", line 54, in import_photos res = Photos.import_photos(photos,options) File "F:\Dev\Python\Photon\photon\model.py", line 130, in import_photos copyright=copyright) File "p:\python25\lib\site-packages\SQLObject-0.9.0b2-py2.5.egg \sqlobject\declarative.py", line 94, in _wrapper return fn(self, *args, **kwargs) File "p:\python25\lib\site-packages\SQLObject-0.9.0b2-py2.5.egg \sqlobject\main.py", line 1214, in __init__ self._create(id, **kw) File "p:\python25\lib\site-packages\SQLObject-0.9.0b2-py2.5.egg \sqlobject\main.py", line 1242, in _create self.set(**kw) File "p:\python25\lib\site-packages\SQLObject-0.9.0b2-py2.5.egg \sqlobject\main.py", line 1106, in set setattr(self, name, value) File "F:\Dev\Python\Photon\photon\model.py", line 52, in _set_keywords self.addKeyword(1) File "<string>", line 1, in <lambda> File "p:\python25\lib\site-packages\SQLObject-0.9.0b2-py2.5.egg \sqlobject\joins.py", line 223, in add getID(inst), File "p:\python25\lib\site-packages\SQLObject-0.9.0b2-py2.5.egg \sqlobject\joins.py", line 15, in getID return int(obj) TypeError: int() argument must be a string or a number, not 'Photo' -------------------------------------------------------------------------------------------------------- I did a little debugging, the add function in sqlobject\joins.py looks like this: ------------------------------------------------------------------ def add(self, inst, other): inst._connection._SO_intermediateInsert( self.intermediateTable, self.joinColumn, getID(inst), self.otherColumn, getID(other)) --------------------------------------------------------------------- debugging shows that it receives <Photo (not initialized)> as inst, and <Keyword 1 keyword='test'> as other. It also sees self.addKeyword in my model as a lambda - <bound method Photo.<lambda> of <Photo (not initialized)>>. I am unable to find that lambda however, and I don't know how to fix this. Any clues? TiNo |
From: Oleg B. <ph...@ph...> - 2007-05-12 17:56:24
|
On Sat, May 12, 2007 at 07:40:04PM +0200, TiNo wrote: > File "F:\Dev\Python\Photon\photon\model.py", line 52, in > _set_keywords > self.addKeyword(1) What is it? Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |
From: TiNo <ti...@gm...> - 2007-05-13 12:53:15
|
Sorry, I copied the wrong traceback. The only difference though is that last line you quoted. it is File "F:\Dev\Python\Photon\photon\model.py", line 53, in _set_keywords self.addKeyword (ref) I tried passing the id of the keyword as well (1), but that gave me the same error... I found out what caused the error though.While debugging I discovered that the the Photo gets passed on as a uninstantiated class. So I have to create the photo first, and then add the keyword! I was trying to pass the keyword to the Photo class on instantiation. 2007/5/12, Oleg Broytmann <ph...@ph...>: > > On Sat, May 12, 2007 at 07:40:04PM +0200, TiNo wrote: > > File "F:\Dev\Python\Photon\photon\model.py", line 52, in > > _set_keywords > > self.addKeyword(1) > > What is it? > > Oleg. > |
From: Oleg B. <ph...@ph...> - 2007-05-13 12:57:20
|
On Sun, May 13, 2007 at 02:53:11PM +0200, TiNo wrote: > I found out what caused the error though.While debugging I discovered that > the the Photo gets passed on as a uninstantiated class. So I have to create > the photo first, and then add the keyword! Does it work now? Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |
From: TiNo <ti...@gm...> - 2007-05-13 14:22:16
|
Yes, it works. instead of: >>> Photo(filename="bla.jpg",keywords="k1,k2") I now do: >>> p = Photo(filename="bla.jpg") >>> p.keywords = "k1,k2" and that works. 2007/5/13, Oleg Broytmann <ph...@ph...>: > > On Sun, May 13, 2007 at 02:53:11PM +0200, TiNo wrote: > > I found out what caused the error though.While debugging I discovered > that > > the the Photo gets passed on as a uninstantiated class. So I have to > create > > the photo first, and then add the keyword! > > Does it work now? > > Oleg. > -- > Oleg Broytmann http://phd.pp.ru/ ph...@ph... > Programmers don't die, they just GOSUB without RETURN. > |