Home > Enterprise >  Cannot assign "(<Qualification: Qualification object (1)>,)": "QualificationApp
Cannot assign "(<Qualification: Qualification object (1)>,)": "QualificationApp

Time:11-16

this is my model of Qualification Approval

class QualificationApproval(models.Model):
    """Model definition for QualificationApproval."""

    # TODO: Define fields here
    qtitle = models.ForeignKey(Qualification, on_delete=models.CASCADE)
    ofEqualCode = models.CharField(max_length=100)
    porposDate = models.DateField()
    anNo = models.IntegerField()
    status = models.CharField(max_length= 50, default="pending")
    sec9 = models.ForeignKey(Sec9, on_delete=models.CASCADE)

    class Meta:
        """Meta definition for QualificationApproval."""

        verbose_name = 'QualificationApproval'
        verbose_name_plural = 'QualificationApprovals'

so here is qtitle is foreign key of qualification the problem is that when I assign the qualification in QualifcationApproval so its give me and error

def sec9edit(request, secId):
    if 'userId' not in request.session:
        return render(request, "front/login.html", {
            "message": "Login Required First"
        })
    user = User.objects.get(pk = request.session['userId'])
    sec1 = Sec1.objects.get(user = user )
    qualification = Qualification.objects.all()
    if request.method == "POST" and secId:
        sec9 = Sec9.objects.get(pk = secId)
        sec9.curriculum = request.POST['curriculum']
        sec9.save()

        qlrn = request.POST.getlist('qualification')
        code = request.POST.getlist('code')
        pdate = request.POST.getlist('pdate')
        anticipated = request.POST.getlist('anticipated')
        j = 0
        qa = QualificationApproval.objects.filter(sec9 = sec9)
        for q in qlrn:
            if q:
                qua = Qualification.objects.get(pk = q.split(',')[0])
                print(type(qa[j].qtitle))
                qa[j].qtitle = qua,
                qa[j].ofEqualCode = code[j],
                qa[j].porposDate = pdate[j],
                qa[j].anNo = anticipated[j],
                qa[j].sec9 = sec9
                qa[j].save()
        messages.success(request, 'Section 9 udpated successfully')
        return HttpResponseRedirect(reverse('addCentre/sec10'))
    else:
        try:
            sec9 = Sec9.objects.get(sec1= sec1)
            qa = QualificationApproval.objects.filter(sec9 = sec9)
        except:
            return render(request, "front/sec9.html", {
                "qualification": qualification
            })
        return render(request, "front/sec9.html", {
            "qualification": qualification,
            "sec9": sec9,
            "qa": qa
        })

I print the qa.qtitle it give me the result <class 'lrnadmin.models.Qualification'> but when I assign it shows this error

ValueError: Cannot assign "(<Qualification: Qualification object (1)>,)": "QualificationApproval.qtitle" must be a "Qualification" instance.

CodePudding user response:

you have additional , at the end

#                 ↓ delete this commas
qa[j].qtitle = qua,        ↓
qa[j].ofEqualCode = code[j],
qa[j].porposDate = pdate[j],
qa[j].anNo = anticipated[j],

should be like this:

qa[j].qtitle = qua
qa[j].ofEqualCode = code[j]
qa[j].porposDate = pdate[j]
qa[j].anNo = anticipated[j]
  • Related