When I attempt this, for some reason I get the following error in the terminal:
ValueError: Cannot assign “<SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x7f75e744f610>>”: “ExamResult.user” must be a “User” instance.
The following is the trackback when I click the “Save” button:
Django version 4.0.5, using settings 'django_project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Internal Server Error: /exams/2/save/
Traceback (most recent call last):
File "/home/deniz/SeniorProject/django_project/vEnv/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/deniz/SeniorProject/django_project/vEnv/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/deniz/SeniorProject/django_project/Exams/views.py", line 96, in save_exam_view
ExamResult.objects.create(exam=exam, user=user, score=score_)
File "/home/deniz/SeniorProject/django_project/vEnv/venv/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/deniz/SeniorProject/django_project/vEnv/venv/lib/python3.8/site-packages/django/db/models/query.py", line 512, in create
obj = self.model(**kwargs)
File "/home/deniz/SeniorProject/django_project/vEnv/venv/lib/python3.8/site-packages/django/db/models/base.py", line 541, in __init__
_setattr(self, field.name, rel_obj)
File "/home/deniz/SeniorProject/django_project/vEnv/venv/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 235, in __set__
ValueError: Cannot assign "<SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x7f8a12d34880>>": "ExamResult.user" must be a "User" instance.
[09/Jul/2022 23:39:58] "POST /exams/2/save/ HTTP/1.1" 500 91098
The following is how the “Save” button on the web page looks:
Looking at this a bit closer, I see the error message is saying:
ValueError: Cannot assign “<SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x7f8a12d34880>>”: “ExamResult.user” must be a “User” instance.
It appears that this request is coming from an “unauthenticated” request. Is this user logged in? If not, then you don’t have a user object in the request. (Or more accurately, what you have is an instance of the AnonymousUser.)
Yes. Good news! I was able to fix the problem. I had to change the name of a variable that linked to one of the attributes for a particular model. I appreciate your help with all of this Mr. Whitesell.