> -----Original Message-----
> From: sqlalchemy@googlegroups.com 
> [mailto:[EMAIL PROTECTED] On Behalf Of maxi
> Sent: 08 February 2008 14:30
> To: sqlalchemy
> Subject: [sqlalchemy] Re: Search in object list by field value
> 
> 
> Thanks Simon,
> I was doing of that manner.
> 
> Now, is advisable implement my own custom collection ? (One what
> implement a "locate" method for example)
> I was reading Custom Collection Implementations on sqlalchemy doc, but
> I'am not very clear over how implement this.
> 
> Any help with this?
> 
> Regards.
> 

It really depends where you want your 'locate' method to appear. The
custom collection classes mentioned in the docs are used for
relationships between mapped classes.

For example, if Person has a one-to-many relationship with Address, via
a property 'addresses', then by default that 'addresses' property is a
list. By creating a custom collection class with your 'locate' method,
you could write code like this:

person = session.query(Person).first()

address = person.addresses.locate('id', 12)

If that is really what you are trying to do, then this collection class
would probably work (untested):

------------------------------------

class searchable_list(list):
    def locate(self, attrname, value):
        for obj in self:
            if getattr(obj, attrname) == value:
                return obj

------------------------------------

Then when you create your 'addresses' relation, you pass
collection_class=searchable_list. However, this doesn't seem massively
useful to me, so perhaps I haven't understood what you're trying to do.

Simon

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to