How to Solve Error Message Cannot resolve keyword into field. Choices are : when running Django Application

Posted on

Introduction

This is an article about how to solve an error message appearing when running a Django-based internal server to run a Django-based application. There is a function exist in a file with the name of ‘views.py’. Inside that file, there is a function with the name of ‘home’. The error itself exist as follows :

(env) C:\django\project>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
February 02, 2022 - 08:56:53
Django version 3.2.6, using settings 'studybud.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Internal Server Error: /
Traceback (most recent call last):
File "C:\python-3.10\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\python-3.10\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\django\project\base\views.py", line 54, in home
rooms = Room.objects.filter(
File "C:\python-3.10\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\python-3.10\lib\site-packages\django\db\models\query.py", line 941, in filter
return self._filter_or_exclude(False, args, kwargs)
File "C:\python-3.10\lib\site-packages\django\db\models\query.py", line 961, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File "C:\python-3.10\lib\site-packages\django\db\models\query.py", line 968, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "C:\python-3.10\lib\site-packages\django\db\models\sql\query.py", line 1393, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\python-3.10\lib\site-packages\django\db\models\sql\query.py", line 1412, in _add_q
child_clause, needed_inner = self.build_filter(
File "C:\python-3.10\lib\site-packages\django\db\models\sql\query.py", line 1265, in build_filter
return self._add_q(
File "C:\python-3.10\lib\site-packages\django\db\models\sql\query.py", line 1412, in _add_q
child_clause, needed_inner = self.build_filter(
File "C:\python-3.10\lib\site-packages\django\db\models\sql\query.py", line 1286, in build_filter
lookups, parts, reffed_expression = self.solve_lookup_type(arg)
File "C:\python-3.10\lib\site-packages\django\db\models\sql\query.py", line 1112, in solve_lookup_type
_, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
File "C:\python-3.10\lib\site-packages\django\db\models\sql\query.py", line 1539, in names_to_path
raise FieldError("Cannot resolve keyword '%s' into field. "
django.core.exceptions.FieldError: Cannot resolve keyword 'name_icontains' into field. Choices are: created, description, host, host_id, id, message, name, topic, topic_id, updated
[02/Feb/2022 08:58:09] "GET / HTTP/1.1" 500 119726
Not Found: /favicon.ico
[02/Feb/2022 08:58:09,815] - Broken pipe from ('127.0.0.1', 65391)

So, the main error is the line which is giving the error message in the above running Django-based service as follows to focus on it :

django.core.exceptions.FieldError: Cannot resolve keyword 'name_icontains' into field. Choices are: created, description, host, host_id, id, message, name, topic, topic_id, updated

Before going on to the solutin, the following is part of the snippet code where it is exactly exist in the file ‘views.py’ :

def home(request):
    q = request.GET.get('q') if request.GET.get('q') != None else ''
    rooms = Room.objects.filter(
        Q(topic__name__icontains=q) |
        Q(name_icontains=q) |
        Q(description__icontains=q)
    )
    topics = Topic.objects.all()
    context = {'rooms': rooms, 'topics': topics}
    return render(request, 'base/home.html', context)

Solution

As searching and going through the ‘views.py’ file there is a syntax error. The error exist on the filter for searching according to keywords. As in the error message, the searching process can only in the following Choices of fields. Those fields are listing the one which is part of the error message. The fields are ‘created, description, host, host_id, id, message, name, topic, topic_id, updated’. Furthermore, there is only two from the filter field available which is actually matched but it is considered as wrong. The first one is ‘name’ and the second one is ‘description’. But apparently, after searching on the snippet code, there is a simple error syntax on it. In order to use ‘icontains’ feature for searching any specific keywords with a non case-sensitive option, it must use ‘double underline’ characters as in ‘fieldname__icontains’. So, to correct the source code above, just edit the ‘Q(name_icontains=q)’ to have a double underscore as ‘Q(name__icontains=q)’. In the end, the revision for the snippet code to solve the error will be editing ‘views.py’ specifically the ‘home’ function as follows :

def home(request):
    q = request.GET.get('q') if request.GET.get('q') != None else ''
    rooms = Room.objects.filter(
        Q(topic__name__icontains=q) |
        Q(name__icontains=q) |
        Q(description__icontains=q)
    )
    topics = Topic.objects.all()
    context = {'rooms': rooms, 'topics': topics}
    return render(request, 'base/home.html', context)

Leave a Reply