python - Django:如何解决“关键字不能是表达式”错误
问题描述
在下面的代码中,capitals_listings_from_latest_Celery_scrape
是一个Listing
模型对象的 QuerySet:
capitals_listings_from_latest_Celery_scrape = capitals_listings_all.filter(date_added.date()=latest_Celery_scrape_date)
但是,我目前得到
“关键字不能是表达式”
由于date_added.date()
部分原因,此行有错误。
我使用的原因.date()
是为了正确评估我需要从时间数据中剥离date_added
并只留下年、月和日。latest_Celery_scrape_date
date_added
如何修复错误?
解决方案
问题是您将调用作为关键字(参数名称),而参数名称应该是标识符:
capitals_listings_from_latest_Celery_scrape = capitals_listings_all.filter(
date_added.date()=latest_Celery_scrape_date
)
如果你使用这样的语法,你确实会得到这个错误,例如:
>>> id(id()=3)
File "", line 1
SyntaxError: keyword can't be an expression
然而,Django 对此有一个查找:__date
[Django-doc]查找。所以你可以像这样查询:
capitals_listings_from_latest_Celery_scrape = capitals_listings_all.filter(
date_added__date=latest_Celery_scrape_date
)
但是请注意,date
时间戳本身就是一个复杂的问题,因为人们可以旨在查找该特定时区内的日期,或当前时区内该时间戳的数据。
__date
如查找文档中所述:
When
USE_TZ
isTrue
,字段在过滤前转换为当前时区。
推荐阅读
- javascript - 石头剪刀布游戏(js) - 如何创建一个圆形功能
- html - 提供用户选项以通过按钮将评估的 Google 脚本 html 模板下载为 pdf
- android - 问题:发现现有项目依赖项中的不一致。之间的版本不兼容: - com.google.firebase:firebase-auth:19.0.0@aar
- python - 我想在 python 中制作 3*3 矩阵格式列表
- typescript - 如何获取我的 npm 模块的 JSdoc 文档,以便在用户的 VScode 中显示函数?
- swift - 如何使用 Swift 获取 DispatchIO 的文件描述符?
- ios - AVPlayerViewController 上的关闭按钮第一次不起作用
- react-native - 在 react-native-table-component 中搜索
- python - 如何使用 .kv 文件在 kivy 中创建下拉列表
- sql-server - 如何连接具有不同列的两个表