Repository: doccano/doccano

Issue:
[Bug report] Error on Django Admin search
How to reproduce the behaviour
---------
![image](https://user-images.githubusercontent.com/1063219/86004768-6edc3f80-ba1c-11ea-8a58-349a57de6fce.png)

Press "Search".

```
backend_1   | Internal Server Error: /admin/api/document/
backend_1   | Traceback (most recent call last):
backend_1   |   File "/src/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
backend_1   |     response = get_response(request)
backend_1   |   File "/src/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
backend_1   |     response = self.process_exception_by_middleware(e, request)
backend_1   |   File "/src/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
backend_1   |     response = wrapped_callback(request, *callback_args, **callback_kwargs)
backend_1   |   File "/src/venv/lib/python3.6/site-packages/django/contrib/admin/options.py", line 606, in wrapper
backend_1   |     return self.admin_site.admin_view(view)(*args, **kwargs)
backend_1   |   File "/src/venv/lib/python3.6/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
backend_1   |     response = view_func(request, *args, **kwargs)
backend_1   |   File "/src/venv/lib/python3.6/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
backend_1   |     response = view_func(request, *args, **kwargs)
backend_1   |   File "/src/venv/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 223, in inner
backend_1   |     return view(request, *args, **kwargs)
backend_1   |   File "/src/venv/lib/python3.6/site-packages/django/utils/decorators.py", line 45, in _wrapper
backend_1   |     return bound_method(*args, **kwargs)
backend_1   |   File "/src/venv/lib/python3.6/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
backend_1   |     response = view_func(request, *args, **kwargs)
backend_1   |   File "/src/venv/lib/python3.6/site-packages/django/contrib/admin/options.py", line 1685, in changelist_view
backend_1   |     cl = self.get_changelist_instance(request)
backend_1   |   File "/src/venv/lib/python3.6/site-packages/django/contrib/admin/options.py", line 744, in get_changelist_instance
backend_1   |     sortable_by,
backend_1   |   File "/src/venv/lib/python3.6/site-packages/django/contrib/admin/views/main.py", line 81, in __init__
backend_1   |     self.queryset = self.get_queryset(request)
backend_1   |   File "/src/venv/lib/python3.6/site-packages/django/contrib/admin/views/main.py", line 439, in get_queryset
backend_1   |     qs, search_use_distinct = self.model_admin.get_search_results(request, qs, self.query)
backend_1   |   File "/src/venv/lib/python3.6/site-packages/django/contrib/admin/options.py", line 1023, in get_search_results
backend_1   |     queryset = queryset.filter(reduce(operator.or_, or_queries))
backend_1   |   File "/src/venv/lib/python3.6/site-packages/django/db/models/query.py", line 892, in filter
backend_1   |     return self._filter_or_exclude(False, *args, **kwargs)
backend_1   |   File "/src/venv/lib/python3.6/site-packages/django/db/models/query.py", line 910, in _filter_or_exclude
backend_1   |     clone.query.add_q(Q(*args, **kwargs))
backend_1   |   File "/src/venv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1290, in add_q
backend_1   |     clause, _ = self._add_q(q_object, self.used_aliases)
backend_1   |   File "/src/venv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1312, in _add_q
backend_1   |     current_negated, allow_joins, split_subq, simple_col)
backend_1   |   File "/src/venv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1318, in _add_q
backend_1   |     split_subq=split_subq, simple_col=simple_col,
backend_1   |   File "/src/venv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1251, in build_filter
backend_1   |     condition = self.build_lookup(lookups, col, value)
backend_1   |   File "/src/venv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1107, in build_lookup
backend_1   |     raise FieldError('Related Field got invalid lookup: {}'.format(lookup_name))
backend_1   | django.core.exceptions.FieldError: Related Field got invalid lookup: icontains
backend_1   | [29/Jun/2020 12:25:49] "GET /admin/api/document/?q=request HTTP/1.1" 500 160618
```

Is this like https://stackoverflow.com/questions/11754877/troubleshooting-related-field-has-invalid-lookup-icontains?

Your Environment
---------
<!-- Include details of your environment.-->
*   Operating System: Calculate Linux 20.6
*   Python Version Used: system Python version is 3.7.7
*   When you install doccano: 4927a01f090b91d8e14e467f2fd40d8301612e72
*   How did you install doccano (Heroku button etc): Docker Compose



Discussion:
Thank you. The link is very helpful.

Resolving patch:
diff --git a/app/api/admin.py b/app/api/admin.py
--- a/app/api/admin.py
+++ b/app/api/admin.py
@@ -9,13 +9,13 @@
 class LabelAdmin(admin.ModelAdmin):
     list_display = ('text', 'project', 'text_color', 'background_color')
     ordering = ('project',)
-    search_fields = ('project',)
+    search_fields = ('text',)
 
 
 class DocumentAdmin(admin.ModelAdmin):
     list_display = ('text', 'project', 'meta')
     ordering = ('project',)
-    search_fields = ('project',)
+    search_fields = ('text',)
 
 
 class ProjectAdmin(admin.ModelAdmin):
@@ -27,19 +27,19 @@ class ProjectAdmin(admin.ModelAdmin):
 class SequenceAnnotationAdmin(admin.ModelAdmin):
     list_display = ('document', 'label', 'start_offset', 'user')
     ordering = ('document',)
-    search_fields = ('document',)
+    search_fields = ('document__text',)
 
 
 class DocumentAnnotationAdmin(admin.ModelAdmin):
     list_display = ('document', 'label', 'user')
     ordering = ('document',)
-    search_fields = ('document',)
+    search_fields = ('document__text',)
 
 
 class Seq2seqAnnotationAdmin(admin.ModelAdmin):
     list_display = ('document', 'text', 'user')
     ordering = ('document',)
-    search_fields = ('document',)
+    search_fields = ('document__text',)
 
 
 class RoleAdmin(admin.ModelAdmin):
@@ -51,7 +51,7 @@ class RoleAdmin(admin.ModelAdmin):
 class RoleMappingAdmin(admin.ModelAdmin):
     list_display = ('user', 'role', 'project', )
     ordering = ('user',)
-    search_fields = ('user',)
+    search_fields = ('user__username',)
 
 
 admin.site.register(DocumentAnnotation, DocumentAnnotationAdmin)


Test patch: