Tag Archives: search_fields

[Solved] Django use search_fields error (in get_lookup_constraint)

django use search_fields error (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: Monday, 22 July 2019 21:07:21 +0800

 

Reason:

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

This is a fuzzy query field in the view. The submitter field is a foreign key ForeignKey field. As a foreign key, it corresponds not to a specific field, but to a class

Therefore, we should map it to a specific field associated with a foreign key, such as submitter__username

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

[Solved] Search with xadmin_ Related field got invalid lookup: icontains

Add these three fields to the class defined in the adminx.py file
list_display = ['code','email','send_type','send_time'] # the type of fields to display
search_fields = ['code','email','send_type'] # fields to search (all fields together) No fields with foreign keys here
list_filter = ['code','email','send_type','send_time'] # fields searched (single field search) and time

1.Problem description

When using xadmin to realize Django background function, use search_ Fields = [field name, field name], related field got invalid lookup: icons

2.Problem analysis

search_ Fields controls the name of the field that can be searched through the search box_ Fields = () or search_ Fields = [], xadmin uses fuzzy query

The search of related fields is invalid because search_ For example, if the field type is ForeignKey, an error will be reported

3.Solutions

Delete search_ Fields, for example, delete the field type ForeignKey