django后台搜索报错:Related Field got invalid lookup: icontains

这个错误一般是由于你在admin.py文件里的search_fields使用了外键,而没有指定具体的字段。

比如:我的外键是author,写成:search_fields = (‘title’, ‘author’, )就会报错,儿应该写成:

search_fields = (‘title’, ‘author__name’, )

完整报错内容:

FieldError at /admin/app1/article/
Related Field got invalid lookup: icontains
Request Method:	GET
Request URL:	http://127.0.0.1:8000/admin/app1/article/?q=1
Django Version:	2.1.5
Exception Type:	FieldError
Exception Value:	
Related Field got invalid lookup: icontains
Exception Location:	C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\query.py in build_lookup, line 1087
Python Executable:	C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe
Python Version:	3.7.0
Python Path:	
['F:\\python\\untitled1',
 'F:\\python\\untitled1',
 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip',
 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',
 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\lib',
 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37',
 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages',
 'D:\\Program Files\\JetBrains\\PyCharm '
 '2018.3.2\\helpers\\pycharm_matplotlib_backend']
Server time:	Wed, 16 Jan 2019 09:18:36 +0000

猜你喜欢

转载自blog.csdn.net/jeesr/article/details/86511553