How to Solve Error Message models.Model.DoesNotExist: Model matching query does not exist in Django

Posted on

This article will show how to solve an error message exist upon executing or running a Django-based web application. Actually, there is a script in the Django-based web application which has a certain line of source code as follows in the file with the name of ‘context_processors.py’. It is a file where in Django-based web application has a specific function to create a specific content for every templates to be available. The following is that line of code :

object_variable = ModelName.objects.get(user=request.user)

It is a line to get the ModelName object by executing a specific query to the tabel associated with the model with the name of ModelName. The query is a specific query where there is a parameter with the name of user. That parameter will have the value from the ‘user’ variable passed from the request. So, since the above content’s execution is done after the login process, basically, it is telling that to thell the username value to be passed to the above source code line. But apparently, the above line code is not working and it generates the following error message instead :

C:\python-win\app\loads\context_processors.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
May 10, 2020 - 19:38:37
Django version 3.0.5, using settings 'app.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-win\app\loads\context_processors.py", line 9, in object_variable
staff = Staff.objects.get(user=request.user)
File "C:\python-win\myenv\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\python-win\myenv\lib\site-packages\django\db\models\query.py", line 415, in get
raise self.model.DoesNotExist(
loads.models.ModelName.DoesNotExist: ModelName matching query does not exist.

There is a specific error message which is stating that the ModelName matching query does not exist. It is because there is no record of that ModelName with the parameter of user variable with the value of the available user variable itself. So, it will generate error message as it is available in the above output execution. It is the “loads.models.ModelName.DoesNotExist: ModelName matching query does not exist.” So, in order to handle, the problem, just execute the command for creating a specific record for the ModelName which has the associated user value.

Leave a Reply