Home > database >  cannot Assign Value , must be an instance django
cannot Assign Value , must be an instance django

Time:06-17

I am getting below error when inserting values through request.post.get method

Cannot assign "'1'": "Faculty_Feedback.department" must be a "Department" instance.

The model in which i want to insert data is

class Faculty_Feedback(models.Model):
    department = models.ForeignKey(Department,on_delete=models.CASCADE)
    program    = models.ForeignKey(Program,on_delete=models.CASCADE)
    resp1 = models.IntegerField(default=1)
    resp2 = models.IntegerField(default=1)
    resp3 = models.IntegerField(default=1)
    resp4 = models.IntegerField(default=1)
    resp5 = models.IntegerField(default=1)
    resp6 = models.IntegerField(default=1)
    resp7 = models.IntegerField(default=1)
    resp8 = models.IntegerField(default=1)
    resp9 = models.IntegerField(default=1)
    resp10 = models.IntegerField(default=1)
    resp11 = models.IntegerField(default=1)
    resp12 = models.IntegerField(default=1)
    resp13 = models.IntegerField(default=1)

I am inserting department and program values into template from below code

departments = Department.objects.all()
    programs= Program.objects.all()
    context = {
        "departments":departments,
        "programs":programs
    }

Below is the template code

<div >
                <label for="text">Department</label>
                <select style="width: 90%"  name="department" id="department">
                    {% for department in departments%}
                    <option value="{{department.pk}}">{{department.dept}}</option>
                    {% endfor%}
                </select>
            </div>

            <div >
                <label for="text">Program</label>
                <select style="width: 90%"  name="program" id="program">
                    {% for program in programs%}
                    <option value="{{program.pk}}">{{program.program}}</option>
                    {% endfor%}
                </select>
            </div>

I am inserting data of feedback by getting values through request.post.get method

def feedback(request):
    if request.method == "POST":
        dept = request.POST.get('department')
        prog = request.POST.get('program')
        q1 = request.POST.get('f1')
        q2 = request.POST.get('f2')
        q3 = request.POST.get('f3')
        q4 = request.POST.get('f4')
        q5 = request.POST.get('f5')
        q6 = request.POST.get('f6')
        q7 = request.POST.get('f7')
        q8 = request.POST.get('f8')
        q9 = request.POST.get('f9')
        q10 = request.POST.get('f10')
        q11 = request.POST.get('f11')
        q12 = request.POST.get('f12')
        q13 = request.POST.get('f13')
        com1 = request.POST.get('com1')
        com2 = request.POST.get('com2')
      

        save_feedback = Faculty_Feedback(department = dept , program = prog,resp1=q1, resp2=q2, resp3=q3, resp4=q4,
                                         resp5=q5, resp6=q6, resp7=q7, resp8=q8,
                                         resp9=q9, resp10=q10, resp11=q11, resp12=q12,
                                         resp13=q13)
        save_comments = Faculty_Comments(department = dept,program = prog,comment_1 = com1 , comment_2 = com2)
        save_feedback.save()
        save_comments.save()

CodePudding user response:

Department and Program should be the object, not the ID.

dept_object = Department.objects.get(id=dept)
program_object = Program.objects.get(id=prog)

save_feedback = Faculty_Feedback(department = dept_object , program = program_object,resp1=q1, resp2=q2, resp3=q3, resp4=q4,
                                     resp5=q5, resp6=q6, resp7=q7, resp8=q8,
                                     resp9=q9, resp10=q10, resp11=q11, resp12=q12,
                                     resp13=q13)

Get the ID from the Request and then Get the object for that id and add it when creating a new record.

  • Related