django使用search_fields报错

django使用search_fields报错(in get_lookup_constraint)

FieldError at /api/workorder/order/

Related Field got invalid lookup: icontains
Request Method:GET
Request URL:http://127.0.0.1:8000/api/workorder/order/?search=1
Django Version:2.0
Exception Type:FieldError
Exception Value:Related Field got invalid lookup: icontains
Exception Location:C:\Users\WM\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\sql\query.py in build_lookup, line 1075
Python Executable:C:\Users\WM\AppData\Local\Programs\Python\Python36\python.exe
Python Version:3.6.5
Python Path:['F:\git_code\emergency_itil\EmergencyResponse', 'F:\git_code\emergency_itil\EmergencyResponse', 'E:\PyCharm 2019.1.1\helpers\pycharm_display', 'C:\Users\WM\AppData\Local\Programs\Python\Python36\python36.zip', 'C:\Users\WM\AppData\Local\Programs\Python\Python36\DLLs', 'C:\Users\WM\AppData\Local\Programs\Python\Python36\lib', 'C:\Users\WM\AppData\Local\Programs\Python\Python36', 'C:\Users\WM\AppData\Roaming\Python\Python36\site-packages', 'C:\Users\WM\AppData\Local\Programs\Python\Python36\lib\site-packages', 'E:\PyCharm 2019.1.1\helpers\pycharm_matplotlib_backend']
Server time:星期一, 22 七月 2019 21:07:21 +0800

异常原因:

search_fields = ('content', 'detail','submitter')

这是在view中模糊查询的字段,其中submitter字段是一个外键ForeignKey字段,而作为一个外键,它所对应的不是一个具体的字段,而是一个类。

所以我们应该将其对应成为一个外键关联的摸一个具体的字段,如submitter__username

search_fields = ('content', 'detail','submitter__username')

转载于:https://www.cnblogs.com/jiumo/p/11228460.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值