Hi
I have created a possibilty to assign a list of dicts to a MultipleJoin
column of an sqlobject and the sub-objects are automatically created.
It works for me in this project but I have no idea if this could be a
general solution for SQLObject?
class BaseObject(SQLObject):
def __setattr__(self, name, newv):
"""Enables setting of MultipleJoin columns.
This deletes all currently by MultipleJoin referenced objects
and creates and assigns new objects
of the referenced type.
Usage: Set the MultipleJoin attribute to a list of dicts.
The dicts will be used to create the attr of the new objects.
"""
try:
multiplejoin = [j for j in self.sqlmeta.joins
if isinstance(j, joins.SOMultipleJoin) and
name == j.joinMethodName][0]
except IndexError:
#call properties directly otherwise delegate attr setting
if isinstance(getattr(self.__class__, name, None), property):
getattr(self.__class__, name).fset(self, newv)
else:
super(BaseObject, self).__setattr__(name, newv)
else:
#delete dependend rows
for oldrow in getattr(self, name):
oldrow.destroySelf() #performance?
#insert new rows
for newrow in newv:
attrdict = {}
for key, value in newrow.iteritems():
attrdict[str(key)] = value
#find ForeignKey
for col in
multiplejoin.otherClass.sqlmeta.columns.values():
if isinstance(col, SOForeignKey):
if col.foreignKey == self.__class__.__name__:
attrdict[col.name] = self.id
multiplejoin.otherClass(**attrdict)
--
Gregor
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
sqlobject-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss