18 May 05:49
Re: filtering by datetime elements
From: Michael Bayer <mike_mp <at> zzzcomputing.com>
Subject: Re: filtering by datetime elements
Newsgroups: gmane.comp.python.sqlalchemy.user
Date: 2008-05-18 03:49:45 GMT
Subject: Re: filtering by datetime elements
Newsgroups: gmane.comp.python.sqlalchemy.user
Date: 2008-05-18 03:49:45 GMT
On May 17, 2008, at 10:53 AM, Eric Abrahamsen wrote:
>
> Hi there,
>
> I'm new to this, so please be patient if I'm a little slow... I'm
> trying to filter Article objects by a datetime field ('pubdate'), and
> expected that I would be able to do something like this:
>
> arts = sess.query(Article).filter(and_(Article.pubdate.year==year,
> Article.pubdate.month==month, Article.id==id)).one()
>
> This gives me:
> AttributeError: 'InstrumentedAttribute' object has no attribute 'year'
>
> Apparently methods on attributes are reserved for sqlalchemy
> internals, and I'm not working with a straight Python object, as I'd
> thought. That makes sense, but can anyone suggest a simple substitute
> for what I'm trying to do here?
two approaches here:
1. use datepart functions. currently these are specific to individual
databases (heres the postgres version):
filter(func.date_part("year", Class.somedate)==year)
2. use comparison/BETWEEN; this has the advantage in that if the
column has an index on it, it can be used:
filter(Class.somedate.between(date(year, 1, 1), date(year + 1, 1, 1)))
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to sqlalchemy <at> googlegroups.com
To unsubscribe from this group, send email to sqlalchemy-unsubscribe <at> googlegroups.com
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---
RSS Feed