|
From: Tracy S. R. <tr...@re...> - 2004-01-09 18:32:38
|
This may be a broader question about relational information design, but
I thought I'd put this out anyway.
I would like to build an object, say a 'node', that has a number of
related 'items' based upon a particular 'context'. In one context, for
example, one particular node may 'contain' a certain of items, but
contain other items in a different context.
Here's how I have it defined in SQLObject. Does anyone see a better
way to do this? Or, is there a better way to design associations
between two sets of things that depends on the settings of a 3rd thing?
class node(SQLObject):
name = StringCol(length=32)
itemAssociations = MultipleJoin('itemAssociation')
def getItems(self, context):
return [ia.item for ia in self.itemAssociations if ia.context ==
context]
def getItems_ALT(self, context):
# note: 'getItems' is 5-10% faster for small sets of items
return [iaq.item for iaq in itemAssociation.select(AND(
itemAssociation.q.nodeID==self.id,
itemAssociation.q.contextID==context.id))]
def associateItem(self, item, context):
return itemAssociation.new(node=self, item=item, context=context)
class itemAssociation(SQLObject):
node = ForeignKey('node')
item = ForeignKey('item')
context = ForeignKey('context')
class item(SQLObject):
name = StringCol(length=32)
class context(SQLObject):
name = StringCol(length=32)
For now, I have a 'compile' method in 'node' that takes a context
object as its first arg and returns a node and its information/items
into a dictionary where its 'items' key is a list of item dictionaries,
too.
Thanks,
--T
|