I need help fixing this weird error please. I have an application whereby a teacher can add assignment, the student can then submit assignment and then the teacher can grade it. Right now, every other functionalities works perfectly well except when grading the result.
Note: "STUDENT 1" is the user whose assignment is to be graded
I understand I am using a string instead of an integer for the foreign key field but how can i get that fixed. is there a way to convert the string to integer or how. I tried this method but it doesn't seems to work. I tried "Student_ID__student_ID" and course__Course_Name which was just going to enter the model and use the field which was a string type and then i believe it should match but i got this error instead "Grade_Student() got an unexpected keyword argument 'Student_ID__student_ID'" and then i changed the "Grade_Student(...)" to Grade_Student.objects.update_or_create(...) and then got this new error "IntegrityError at /en/grading_submit NOT NULL constraint failed: schoolapp_grade_student.Student_ID_id" and then i tried fixing it by changing Grade_Student_data.save() to Grade_Student_data.save(commit=False) but it was still same error. Is there anymore information you need to help me please.
Error log
Traceback (most recent call last):
File "C:\Users\Habib\Documents\django\FIVERR\Ayyub_SMS\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Habib\Documents\django\FIVERR\Ayyub_SMS\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Habib\Documents\django\FIVERR\Ayyub_SMS\sms\schoolapp\views.py", line 1948, in grading_submit
Assignment_name=grade_field_assignment_name, Grade=grade_field, Out_Of_Grade=out_grade_filed)
File "C:\Users\Habib\Documents\django\FIVERR\Ayyub_SMS\venv\lib\site-packages\django\db\models\base.py", line 483, in __init__
_setattr(self, field.name, rel_obj)
File "C:\Users\Habib\Documents\django\FIVERR\Ayyub_SMS\venv\lib\site-packages\django\db\models\fields\related_descriptors.py", line 220, in __set__
self.field.remote_field.model._meta.object_name,
Exception Type: ValueError at /en/grading_submit
Exception Value: Cannot assign "'STUDENT 1'": "Grade_Student.Student_ID" must be a "add_students_by_manager" instance.
models.py
class add_courses(models.Model):
Course_Name = models.CharField(max_length=200, blank=True)
Manager_Name = models.ForeignKey(Manager_login_information, on_delete=models.CASCADE, blank=True)
student = models.ManyToManyField(add_students_by_manager, blank=True)
# Start Habib
start_date = models.DateField()
end_date = models.DateField()
# End Habib
def __str__(self):
return self.Course_Name
class add_students_by_manager(models.Model):
manager_ID = models.ForeignKey(Manager_login_information, on_delete=models.CASCADE)
student_ID = models.CharField(max_length=200)
student_name = models.CharField(max_length=200)
def __str__(self):
return self.student_name
class Grade_Student(models.Model):
Student_ID = models.ForeignKey(add_students_by_manager, on_delete=models.CASCADE)
course = models.ForeignKey(add_courses, on_delete=models.CASCADE)
Assignment_name = models.CharField(max_length=200)
Grade = models.IntegerField()
Out_Of_Grade = models.IntegerField()
def __str__(self):
return str(self.Student_ID) + " scored " + str(self.Grade) + " out of " + str(self.Out_Of_Grade) + " in " + str(self.course)
views.py
def grading_submit(request):
if request.method=="POST":
grade_field_stu_id = request.POST.get('grade_field_stu_id')
grade_field_course_name = request.POST.get('grade_field_course_name')
grade_field_assignment_name = request.POST.get('grade_field_assignment_name')
grade_field = request.POST.get('grade_field')
out_grade_filed = request.POST.get('out_grade_filed')
print(grade_field, grade_field_stu_id, grade_field_course_name, grade_field_assignment_name, out_grade_filed)
Grade_Student_data= Grade_Student(Student_ID=grade_field_stu_id, course=grade_field_course_name,
Assignment_name=grade_field_assignment_name, Grade=grade_field, Out_Of_Grade=out_grade_filed)
Grade_Student_data.save()
messages.success(request, _("You Successfully Grade a student!"))
return redirect('assignment_page')
else:
return redirect('/')
grading_page.html
<form class="container pt-5" action="{% url 'grading_submit' %}" method="POST">{% csrf_token %}
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label">Grade</label>
<input name="grade_field" type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
<input name="grade_field_stu_id" value="{{stu_id_grade}}" type="hidden" class="form-control">
<input name="grade_field_course_name" value="{{stu_course_grade}}" type="hidden" class="form-control">
<input name="grade_field_assignment_name" value="{{stu_ass_name_grade}}" type="hidden" class="form-control">
<input name="out_grade_filed" value="{{stu_total_grade}}" type="hidden" class="form-control">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>