英文:
Whats the proper way to use date and interval in SQLAlchemy?
问题
date_filter = (datetime.today() - timedelta(days=7)).strftime('%Y-%m-%d')
.filter(DayManifestBatch.manifest_date >= cast(date_filter, DATE))
英文:
I need to implement this filter in SQLalchemy, with a MySQL database
dmb.manifest_date >= (CURDATE() - interval 7 day)
What I did is
date_filter=(datetime.today()-timedelta(days=7)).strftime('%Y-%m-%d')
.filter(DayManifestBatch.manifest_date>= cast(date_filter,DATE))
I imported DATE and cast from SQLALchemy. Also tried with just the string date and datetime format but I always get this error
sqlalchemy.exc.ArgumentError: SQL expression object expected, got object of type <class 'flask_sqlalchemy.model.DefaultMeta'> instead
</details>
# 答案1
**得分**: 0
也许有更好的方法,但你可以使用:
```python
from sqlalchemy import text
.filter(DayManifestBatch.manifest_date >= text('CURDATE() - INTERVAL 7 DAY'))
英文:
There is probably a better way but you can use:
from sqlalchemy import text
.filter(DayManifestBatch.manifest_date >= text('CURDATE() - INTERVAL 7 DAY'))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论