I solved this problem in the end, by spending a lot of time trying to guess the
valid contents of
PlayerForSale.q.*
As it turns out, against all probability, it was PersonForSale.q.ID that I
wanted. I used this to
write this beautiful code which searches arbitrary and optional fields in the
database:
def transfer_search(self, **kw):
terms = [PlayerForSale.q.playerID == Player.q.id]
colstoshow = []
for attr, bounds in kw.iteritems():
if type(bounds) is not dict:
continue
for bound, value in bounds.iteritems():
if not value:
continue
colstoshow.append(attr)
if bound == "min":
terms.append(getattr(Player.q, attr) >= value)
if bound == "max":
terms.append(getattr(Player.q, attr) <= value)
results = PlayerForSale.select(AND(*terms),
orderBy=PlayerForSale.q.askingprice)
return dict(
title="Transfer Search: %s results" % (results.count()),
results = results,
cols=[(col, col.title()) for col in set(colstoshow)])
It evens modifies the results shown in the table to only show columns that user
searched against. As
there are 14 variables to search against + 4 others the table always shows,
this made sense. But
eventually the __str__ method of Player will build a tooltip showing all the
relevant information
anyway.
I hope someone finds this code useful.
-Rob
PS. TG has warts but it's still the beauty to PHP's beast.
Robin Haswell wrote:
> Hey guys
>
> I was wondering if you could give me a hand with an SO problem I've run in
> to. I'm building a search
> mechanism for my database. This is what my model (essentially) looks like:
>
> class Player(SQLObject):
> firstname = StringCol(length=255)
> lastname = StringCol(length=255)
> fullname = DatabaseIndex("firstname", "lastname", unique=True)
>
> # ..snip..
>
> passing = IntCol()
>
> forsale = SingleJoin("PlayerForSale") # Don't really want this here,
> thought I might as well
>
> class PlayerForSale(SQLObject):
> player = ForeignKey("Player", alternateID=True)
> askingprice = IntCol()
>
> Anyway I'm trying to do .select to get some data, but I can't seem to make
> comparisons properly.
> Some examples:
>
> >>> list(PlayerForSale.select(AND(PlayerForSale.q.player_id == Player.q.id,
> Player.q.passing > 1)))
> Traceback (most recent call last):
> File "<console>", line 1, in ?
> File
> "/usr/lib/python2.4/site-packages/SQLObject-0.7.1dev_r1675-py2.4.egg/sqlobject/sqlbuilder.py",
> line
> 350, in __getattr__
> raise AttributeError("%s instance has no attribute '%s'" %
> (self.soClass.__name__, attr))
> AttributeError: PlayerForSale instance has no attribute 'player_id'
> >>> list(PlayerForSale.select(AND(PlayerForSale.q.player == Player.q.id,
> Player.q.passing > 1)))
> Traceback (most recent call last):
> File "<console>", line 1, in ?
> File
> "/usr/lib/python2.4/site-packages/SQLObject-0.7.1dev_r1675-py2.4.egg/sqlobject/sqlbuilder.py",
> line
> 350, in __getattr__
> raise AttributeError("%s instance has no attribute '%s'" %
> (self.soClass.__name__, attr))
> AttributeError: PlayerForSale instance has no attribute 'player'
> >>>
>
>
> As you can see there are no PlayerForSale.q.* attributes for my ForeignKey,
> which is a real bummer.
> I can see this field in the database as well, so it's noting to do with that
>
> The closest I got was this:
>
> >>> len(list(Player.select(AND(Player.q.passing >= 1, Player.forsale > 0))))
> 34
>
> But that's not even nearly close as:
>
> >>> len(list(PlayerForSale.select()))
> 13
>
> I think that's just plain way off
>
> Can anyone help me with this? I'm totally stumped and a bit disappointed :-/
>
> Cheers
>
> -Rob
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---