On Tue, 2013-01-22 at 09:26:35 +0200, Alexey Vihorev wrote: > 1. Does SQLA support "deep filtering", i.e. something like this: > > query(Invoice).filter(Invoice.Customer.Country.name=='France') > > This does not work as it is, but is there something along this lines (except > of going with query.filter(.query.filter(.query.filter(query.get()))))?
SQLAlchemy tries to follow SQL closely, hence the "SQL" in its name. So
it's helpful to think in terms of SQL when trying to construct a query.
In this case you would need to use JOINs:
# Assuming that you have all these tables with properly defined
# relations.
session.query(Invoice).join(Customer, Country).\
filter(Country.name == u'France')
> 2.Can I use hybrid properties for filtering? I tried to do that, but that's
> what I got:
>
> class Person(Base):
> first_name = Column(String(50))
> last_name = Column(String(50))
>
> def __init__(self, first, last):
> self.first_name = first
> self.last_name = last
>
> @hybrid_property
> def full_name(self):
> print(self.first_name, self.last_name)
> return self.first_name + " " + self.last_name
>
> p = Person('John', 'Doe')
> s.commit()
> res = s.query(Person).filter(Person.full_name=='John Doe').all()
>
> output:
>
> Person.first_name Person.last_name
> []
>
> Apparently, Person.full_name receives a class as an argument instead of an
> instance. Is there other way?
That's what you want in this case, that this hybrid property would get a
class, because Query works with classes. Try adding echo=True to
create_engine() to see the actual SQL emitted, it should be correct.
In your example you forgot to add newly created p object to a session:
s.add(p)
s.commit()
Your Person class is missing __tablename__ as well.
--
Audrius Kažukauskas
http://neutrino.lt/
pgp5HoNMwVzps.pgp
Description: PGP signature
