Eric Abrahamsen | 17 May 16:51
Picon

filtering by datetime elements


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?

Thanks very much,
Eric

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

az | 17 May 17:17

Re: filtering by datetime elements


On Saturday 17 May 2008 17:53:42 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?
it's SQL that is not working with python objects, and the column 
pubdate (associated with type DateTime on python side) has no 
attr .year or .month.

lookup the messages in the group, there were some sugestions long time 
ago, but AFAIremember one was something with strings, another with 
separate columns.

wishlist suggestion: how about type-related attributes on columns, 
using which will autogenerate some expr off that column?
e.g. select( mytable.c.mydate.year == '1998') to automagicaly yield 
(Continue reading)

Eric Abrahamsen | 17 May 18:20
Picon

Re: filtering by datetime elements


On May 17, 2008, at 11:17 PM, az <at> svilendobrev.com wrote:
> it's SQL that is not working with python objects, and the column
> pubdate (associated with type DateTime on python side) has no
> attr .year or .month.
>
> lookup the messages in the group, there were some sugestions long time
> ago, but AFAIremember one was something with strings, another with
> separate columns.

Thanks svil, this is good to know. I suppose there's no reason why I  
can't pull a simpler query into Python and then filter it by date  
there. It seems like doing this in the SQL query is going to be  
hackish no matter what, particularly when it's so simple to do in  
Python...

Thanks again,
Eric

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

Lukasz Szybalski | 17 May 21:56
Picon

Re: filtering by datetime elements


On Sat, May 17, 2008 at 11:20 AM, Eric Abrahamsen <girzel <at> gmail.com> wrote:
>
>
> On May 17, 2008, at 11:17 PM, az <at> svilendobrev.com wrote:
>> it's SQL that is not working with python objects, and the column
>> pubdate (associated with type DateTime on python side) has no
>> attr .year or .month.
>>
>> lookup the messages in the group, there were some sugestions long time
>> ago, but AFAIremember one was something with strings, another with
>> separate columns.
>
> Thanks svil, this is good to know. I suppose there's no reason why I
> can't pull a simpler query into Python and then filter it by date
> there. It seems like doing this in the SQL query is going to be
> hackish no matter what, particularly when it's so simple to do in
> Python...
>

you could convert your month variable to same format as
article.pubdate (datetime)
http://lucasmanual.com/mywiki/PythonManual#head-7b8d3475aa2baaa193b02b72fccd6eb009a1ee63

or modify the datetime to separate date and time columns.
Lucas

--

-- 
Automotive Recall Database. Cars, Trucks, etc.
http://www.lucasmanual.com/recall/
(Continue reading)

Roger Demetrescu | 27 May 16:56
Picon

Re: filtering by datetime elements


On Sat, May 17, 2008 at 12:17 PM,  <az <at> svilendobrev.com> wrote:

> wishlist suggestion: how about type-related attributes on columns,
> using which will autogenerate some expr off that column?
> e.g. select( mytable.c.mydate.year == '1998') to automagicaly yield
> something like func.substr(mytable.c.mydate,1,4)) == '1998'
> (supposing the type of mydate stores datetime as yyyymmdd string)

That sounds interesting...

+1 on that...  :)

Michael, is that feasible / desirable for SA 0.5 ?

Cheers,

Roger

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

Michael Bayer | 27 May 17:14

Re: filtering by datetime elements


On May 27, 2008, at 10:56 AM, Roger Demetrescu wrote:

>
> On Sat, May 17, 2008 at 12:17 PM,  <az <at> svilendobrev.com> wrote:
>
>> wishlist suggestion: how about type-related attributes on columns,
>> using which will autogenerate some expr off that column?
>> e.g. select( mytable.c.mydate.year == '1998') to automagicaly yield
>> something like func.substr(mytable.c.mydate,1,4)) == '1998'
>> (supposing the type of mydate stores datetime as yyyymmdd string)
>
> That sounds interesting...
>
> +1 on that...  :)
>
>
> Michael, is that feasible / desirable for SA 0.5 ?

not really since its fairly arbitrary and redundant against using  
func.XXX().    Anytime we start sticking .XXX on an object of some  
type, we start hitting a namespace problem, and magical stuff like  
this invariably causes many more problems than it solves (witness  
assign_mapper()).   This one is a lot worse than assign_mapper() since  
the specific group of names available would be different based on  
column type.  if you think of all the functions that could be had off  
of all the different datatypes across all the different database  
implementations, this becomes an enormous job.

Also the usual use case for dateparts is that of comparison to a  
(Continue reading)

Roger Demetrescu | 27 May 17:36
Picon

Re: filtering by datetime elements


On Tue, May 27, 2008 at 12:14 PM, Michael Bayer
<mike_mp <at> zzzcomputing.com> wrote:
>
>
> On May 27, 2008, at 10:56 AM, Roger Demetrescu wrote:
>
>>
>> On Sat, May 17, 2008 at 12:17 PM,  <az <at> svilendobrev.com> wrote:
>>
>>> wishlist suggestion: how about type-related attributes on columns,
>>> using which will autogenerate some expr off that column?
>>> e.g. select( mytable.c.mydate.year == '1998') to automagicaly yield
>>> something like func.substr(mytable.c.mydate,1,4)) == '1998'
>>> (supposing the type of mydate stores datetime as yyyymmdd string)
>>
>> That sounds interesting...
>>
>> +1 on that...  :)
>>
>>
>> Michael, is that feasible / desirable for SA 0.5 ?
>
> not really since its fairly arbitrary and redundant against using
> func.XXX().

<SNIP>

Ok... thanks for the answer Michael...

(Continue reading)

Michael Bayer | 18 May 05:49

Re: filtering by datetime elements


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:
(Continue reading)


Gmane