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

时间: 2024-10-09 21:38:21

django使用search_fields报错的相关文章

记一次django的诡异报错 Could not parse the remainder: '=' from '='

如题,一个展示日志的功能,调用该模板文件时报错,Could not parse the remainder: '=' from '='.这行模板语言在上面出现过同样的,仅仅是改了'<='右端的整数大小,而且该有的标签也不少. 百般思考无解,stackoverflow和百度都没答案.后来重新看了下开发文档,发现是Django的版本不对,开发时用的1.8.2,我部署时用的1.11.5.重新安装低版本的django后问题解决.但原因仍待查. 记一次django的诡异报错 Could not parse

Django urls.py报错: raise TypeError(&#39;view must be a callable or a list/tuple in the case of include()

Django urls.py报错: raise TypeError('view must be a callable or a list/tuple in the case of include() 报错信息 "F:\PyCharm 2016.2.2\bin\runnerw.exe" C:\Users\Administrator\AppData\Local\Programs\Python\Python35\python.exe F:/Django/blogs/manage.py run

Django——模版Template报错

>>> from django.template import Template >>> t = Template("My name is {{ my_name }}.") Traceback (most recent call last): File "C:\Python33\lib\site-packages\django\conf\__init__.py", line 38, in _setup settings_modul

django admin 中文报错incorrect string value解决方案

对于错误" Incorrect string value: '\xE6\xA2\xB5\xE8\x92\x82...'for column 'object_repr' at row 1 解决方法是设置django_admin_log表的object_repr一项使用utf8_unicode_ci: 对于错误"  Incorrect string value: '\xE6\xA2\xB5\xE8\x92\x82...'for column 'change_message' at row

Django 使用allauth报错 RuntimeError: Model class django.contrib.sites.models.Site doesn&#39;t declare an explicit app_label and isn&#39;t in an application in INSTALLED_APPS

一:报错 RuntimeError: Model class django.contrib.sites.models.Site doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS 出现这种情况需将Django的Sites框架添加到您的应用程序中,并在您的设置中将SITE_ID设置为1即可.位置放在默认配置的后面,其他应用的前面. INSTALLED_APPS = [ 'django

Django:django-cors-headers 报错no module named &quot;corsheaders&quot;

django跨域使用 pip install django-cors-headers 然后在settings文件中加上参数设置 # app配置 INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles

pycharm无法新建django项目,报错timed out,需先建立pure项目,进入后建立django项目,最后调整目录层级。

一.pycharm无法新建django项目,报错timed out,需先建立pure项目,名字改成mysite: 二.进入后,在下方Terminal里,输入: pip install Django==2.2.7 用来安装指定版本的django 三.继续在下方Terminal里,输入: django-admin startproject mysite 创建一个名叫mysite的django项目. 四.执行后,你会发现目录结构有变化 点击右边的add configuration,然后点击弹出来窗口的

Django 中文注释报错问题

views.py 前面一定要加# -*- coding: utf-8 -*-否则报错 # -*- coding: utf-8 -*- from django.shortcuts import render_to_response def index(req):     return render_to_response('index.html',{}) # 第一种方式 # 回送模板(第一个return),回送标题(req下直接return字符串) # from django.http impor

django 连接mysql报错

原因: 问题1. 即从mysql5.7版本之后,默认采用了caching_sha2_password验证方式. 问题2.  然后在执行 python manage.py makemigrations依然报错,如下图2/3   raise errorclass(errno, errval)   django.db.utils.OperationalError: (1045, u"Access denied for user 'root'@'localhost' (using password: Y