Thread: [SQLObject] ANN: SQLObject 0.5.2
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Ian B. <ia...@co...> - 2004-03-09 06:26:23
|
SQLObject 0.5.2 released ------------------------ Homepage: http://sqlobject.org Download: http://prdownloads.sourceforge.net/sqlobject/SQLObject-0.5.2.tar.gz? download News: http://sqlobject.org/docs/News.html#sqlobject-0-5-2 What's Changed? --------------- 0.5.2 is a bug fix release, particularly a thread-related bug when using extended selects. All users are recommended to upgrade. What is it? ----------- SQLObject is an object relational mapper. It allows you to easily create an object interface to your database -- tables are classes, rows are instances. Its goals are ease of use and low barrier to entry, while maintaining the ability to customize and extend your classes as your needs grow. -- Ian Bicking | ia...@co... | http://blog.ianbicking.org |
From: charles b. <li...@st...> - 2004-03-11 07:37:11
|
Hi List, 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... -Charles. |
From: Ian B. <ia...@co...> - 2004-03-11 08:01:57
|
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 |
From: Luke O. <lu...@me...> - 2004-03-11 16:18:16
|
Sounds like you might be looking for the alternateID=True argument for columns: email = StringCol(alternateID=True) will add an access method to Person so that: user = Person.byEmail('ab...@xy...') will work. - Luke Quoting charles brandt <li...@st...>: > 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). |
From: charles b. <li...@st...> - 2004-03-12 10:40:04
|
Thank you Luke and Ian! alternateID was exactly what I was looking for. It always seems so obvious after the fact. :) Would it make sense to mention it earlier in the docs when talking about fetching instances that already exist? Something like the following maybe? "You can use the class method .get() to fetch instances that already exist. So if you wanted to fetch the Person by id 10, you'd call Person.get(10). To fetch using data from other unique columns, specify alternateID=True in that column's initialization and use the class method .by<ColumnName>(). (see alternateID for more detail)" Ian's lookup class example was also what I had in mind. I think there was one minor typo in the select that I fixed below, incase others try that. > > class Person(SQLObject): > def lookup(cls, email, connection=None): > return cls.select(cls.q.email=email, connection=connection)[0] > lookup = classmethod(lookup) > I need to read up on new class styles now.. :) Thanks for the pointers... -Charles. |