23

The result should be Date object

Since the day cannot be 'removed', set it to say, 1st day of the month.

Leaving only Month, Year

4

2 に答える 2

46

You can use following constructs to filter the Date column using either year or month:

.filter(extract('year', Foo.Date) == 2012)
.filter(extract('month', Foo.Date) == 12)

And group_by is also possible:

.group_by(sqlalchemy.func.year(Foo.Date), sqlalchemy.func.month(Foo.Date))

Now I haven't tested it, but I assume that this might be slow because these queries result in full table scans, therefore I suggest you invest some time and learn how to use composite columns.

Note: extract() is imported from sqlalchemy.sql

于 2012-08-19T06:13:59.640 に答える
0

if you declare your schema like this,

schema.Column('created', types.TIMESTAMP(), default=now()),

the function now() is defined in the same module which can be like this

def now():
    now_date = datetime.datetime.now()
    return now_date.replace(day=1)

as per group_by requirement, here is a method mentioned.

you can modify hour and other field similarly.

于 2012-08-18T16:08:01.973 に答える