On Mar 11, 2004, at 1:19 AM, charles brandt wrote:
> Sorry for the newbie question, but I'm having a hard time finding the
> right
> way to fetch an existing class instance using a different column than
> id.
> Assuming the example Person class with an added "email = StringCol()",
> I am
> able to fetch based on the email using something like:
>
> user = Person.select(Person.q.email=="ab...@xy...",
> connection=req.conn)[0]
>
> but that seems rather unintuitive compared to Person(10) (or
> Person.get(10)
> now). I think I'm envisioning an overridden _init but the magic of
> the
> metaclass is confusing me on how to implement it. I was also thinking
> something along the lines of a class function (ie
> user=Person.lookup("ab...@xy...", req.conn) ) would be nice, but since
> it is
> actually being initialized I don't think this is correct.
>
> Any ideas/comments? Thanks in advance...
class Person(SQLObject):
email = StringCol(alternateID=True)
user = Person.byEmail("ab...@xy...")
Or just:
user = Person.select(email="ab...@xy...")[0]
Or:
class Person(SQLObject):
def lookup(cls, email, connection=None):
return cls.select(email=email, connection=connection)[0]
lookup = classmethod(lookup)
--
Ian Bicking | ia...@co... | http://blog.ianbicking.org
|