|
From: John B. <jb...@te...> - 2003-11-11 17:51:03
|
On Tuesday 11 November 2003 17:41, Ian Bicking wrote:
> 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.
Well attributes in area would be nice, but it'll do for now :)
--
John Baker, Senior Developer, TEAM.
http://www.teamenergy.com
Views expressed in this mail are my own.
|