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
Bookmark this question. Show activity on this post. class Posts(Base): __tablename__ = 'posts_posts' id = Column(Integer, primary_key = True) user_id = Column(Integer, nullable=False) body = Column(Text, nullable=True) created_at = Column(Date) << is this right?
As the documentation says, all() returns the result of the query as a list.
method sqlalchemy.orm.Query. all() Return the results represented by this Query as a list. This results in an execution of the underlying SQL statement. The Query object, when asked to return either a sequence or iterator that consists of full ORM-mapped entities, will deduplicate entries based on primary key.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With