|
From: Ian B. <ia...@co...> - 2003-11-11 17:41:59
|
On Nov 11, 2003, at 11:20 AM, John Baker wrote:
> Ok :-)
>
> So please provide me with some code that will do my
> Ward/Constituency/Area
> and 'something has many Areas' example... :)
def oneOrNone(val):
try:
return val[0]
except IndexError:
return None
class Area(SQLObject):
# Really should be a one-to-one join for these...
def _get_ward(self):
return oneOrNone(Ward.selectBy(area=self))
def _get_consituency(self):
return oneOrNone(Constiuency.selectBy(area=self))
...
class Ward(SQLObject):
area = ForeignKey('Area')
...
Ta-da! More work if you want to fold the attributes of the connected
tables into the original Area object. I'm sure you won't be satisfied
by this, but it's only bad if you come into the problem with
inheritance fixed in your mind. I tend to come at the problem thinking
in terms of related objects, which works fine right now, and with which
you can handle the same problems, even if in a different way.
--
Ian Bicking | ia...@co... | http://blog.ianbicking.org
|