My backend setting as following
- Postgres 9.5
- Python 2.7
- Django 1.9
I have a table with datetime type, which named createdAt
. I want to use Django ORM to select this field with only date part and group by createdAt
filed.
For example, this createdAt
filed may store 2016-12-10 00:00:00+0
、2016-12-11 10:10:05+0
、2016-12-11 17:10:05+0
... etc。
By using Djanggo ORM, the output should be 2016-12-10
、2016-12-11
。The corresponding sql should similar to: SELECT date(createdAt) FROM TABLE GROUP BY createdAt
.
Thanks.
Django provide
Manager.raw()
to perform raw queries on model but as raw query must contains primary key that is not useful in your case. In caseManager.raw()
is not quite enough you might need to perform queries that don’t map cleanly to models, you can always access the database directly, routing around the model layer entirely by usingconnection
.connection
object represent the default database connection. So you can perform you query like.After executing query you can iterate over cursor object to get query result.