Matt Good wrote:
If you're only getting 1 record the call to count() may actually be more
costly than selecting the record, so you should still avoid it.  You
could probably do this a few ways, but catching the IndexError seems the
cleanest to me:

try:
    res = Person.select()[0]
except IndexError:
    return default
else:
    return res.someAttr

It might save people confusion if bool(SelectResults) Just Worked(TM),
but I think the extra database trip will always be unnecessary.

I think this is only in the trunk, but there you can do:

  obj = Person.select().getOne(default)

This also test that there isn't more than one object, which is a test that is often left out but also generally expected. If you don't pass in a default you get an exception if there is no object.

I don't think that exactly fits the original idea, though, where list(Person.select()[:1]) is probably the best/fastest way to get the first item if one exists.

--
Ian Bicking  /  [EMAIL PROTECTED]  /  http://blog.ianbicking.org


-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
sqlobject-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to