On Nov 20, 2007 11:51 AM, svilen <[EMAIL PROTECTED]> wrote:
>
>
> On Tuesday 20 November 2007 11:37:29 Gaetan de Menten wrote:
> > Hi people,
> >
> > I have some classes with "standard python" properties which target
> > another python object and also uses several columns in the
> > database. I also got a global factory function to create an
> > instance of that target object out of the value of the columns (the
> > class of that target object can vary).
> >
> > Now, I'd like to use those properties in filter criteria, as so:
> >
> > session.query(MyClass).filter(MyClass.my_prop == value)...
> > session.query(MyClass).filter_by(my_prop_name=value)...
> >
> > I've tried using composite properties for that (passing the factory
> > function instead of a composite class), and it actually works, but
> > I'm a little nervous about it: can I have some bad side effect
> > provided that in *some* cases (but not always) the target object is
> > loaded from the database.
> >
> > I also dislike the fact I have to provide a __composite_values__ on
> > all the possible target classes, while in my case, I would prefer
> > to put that logic on the property side. I'd prefer if I could
> > provide a callable which'd take an instance and output the tuple of
> > values, instead of the method. Would that be considered a valid use
> > case for composite properties or am I abusing the system?
> >
> > I've also tried to simply change those properties to descriptors so
> > that I can override __eq__ on the object returned by accessing the
> > property on the class. This worked fine for "filter". But I also
> > want to be able to use filter_by. So, I'd wish that
> > query(Class).filter_by(name=value) would be somehow equal to
> > query(Class).filter(Class.name == value), but it's not. filter_by
> > only accepts MapperProperties and not my custom property.

> what would query(Class).filter(Class.name == value) mean in the case
> of plain property?

Nothing. But in the case of a descriptor, you can do whatever you want
with the class property (as opposed to with a standard property(fget,
fset) where you are can only take care of instance values).

> the Class.name == value would selfconvert to
> SA.expression?

Indeed. That's very easy to do though.

> then what if the filter_by(**kargs) is simply doing
>  _and( getattr( Class, k) == v for k,v in kargs,items() )

That is exactly what I would need in this case BUT I'm not sure it's a
good idea to do it, because I'm not sure it's possible to get that
behavior alongside with the joinpoint thing (which is a good thing
IMO). And it would certainly break other stuff too, so that's probably
not worth the trouble if there is no way to get that behavior in
addition to what already exist.

> i dont understand the _composite bit, why u have to bother with that?

Because, that's another way to do what I want, which actually works
(contrary to the "python property" solution which doesn't work in the
filter_by scenario) but I'm not sure it's the "right" way to go.

-- 
Gaƫtan de Menten
http://openhex.org

--~--~---------~--~----~------------~-------~--~----~
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