I've written class that defines a tree structure via JOINs to the same table, 
as mentioned in the SQLObject docs.  Works great.

Now I wish to create a natural syntax to traverse the tree, 
e.g. 'parent.child1.grandchild2'.  I use the setattr() function for this, and 
it also works well.

However, when i try to override the init method in order to make sure that the 
appropriate setattr() functions get called automatically upon instantiation, 
it doesn't work.  It freezes in an endless loop.

I'm hoping for:
a) more understanding of how the freeze happens, and
b) a way to get around this.  I want the object's attributes always to reflect 
the dynamically-changing linkages in the tree.

Thanks in advance for any insight.  Code follows.

Chris

==========

class ChartAccount(sqlobject.SQLObject):
        number = sqlobject.IntCol(notNone=True)
        name = sqlobject.StringCol(length=128, notNone=True)
        abbreviation = sqlobject.StringCol(length=32, notNone=True)
        parent = sqlobject.ForeignKey('ChartAccount', default=None)
        
        def _init(self, *args, **kw):
                sqlobject.SQLObject._init(self, *args, **kw)
                self.childRegistry = []
                # The following causes a freeze
                #self.registerChildren()
                
        def _get_children(self):
                alias = sqlobject.sqlbuilder.Alias(
                                ChartAccount,"chart_account_alias")
                results = ChartAccount.select(sqlobject.AND(
                                ChartAccount.q.parentID==alias.q.id,
                                alias.q.id==self.id))
                return list(results)

        def registerChildren(self):
                for link in self.childRegistry:
                        delattr(self, link)
                self.childRegistry = []
                for child in self.children:
                        setattr(self, child.abbreviation, child)
                        self.childRegistry.append(child.abbreviation)

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to