“ <StudentProfile:None None None>”:“ StudentsSubmittedDocument.Students_Enrollment_Records”必须是“ StudentsEnrollmentRecord”实例

时间:2019-10-17 13:34:29

标签: django

views.py

    id = request.POST.get('ids')
    studentname = StudentProfile(id=id)

    myfile = request.FILES['myfile']
    fs = FileSystemStorage()
    filename = fs.save(myfile.name, myfile)
    uploaded_file_url = fs.url(filename)
    student = StudentsEnrollmentRecord.Student_Users

    V_insert_data = StudentsEnrollmentRecord(
            Student_Users=studentname, Payment_Type=payment,
            Education_Levels=educationlevel,School_Year=schoolyear
        )
        V_insert_data.save()
        insert_doc = StudentsSubmittedDocument(
            Students_Enrollment_Records = studentname,
            Document = myfile
        )
        insert_doc.save()

    return render(request, 'Homepage/pending.html')

models.py

class StudentProfile(models.Model):
    DoesNotExist = None
    objects = None
    Pending_Request = [
        ('Pending_Request', 'Pending_Request'),
        ('Enrolled', 'Enrolled'),
    ]
    Image = models.ImageField(upload_to='images',null=True,blank=True)
    Username = models.CharField(max_length=500,null=True,blank=True)
    Password = models.CharField(max_length=500,null=True,blank=True)
    LRN = models.IntegerField(null=True,blank=True)
    Firstname = models.CharField(max_length=500,null=True,blank=True)
    Middle_Initial = models.CharField(max_length=500,null=True,blank=True)
    Lastname = models.CharField(max_length=500,null=True,blank=True)

class StudentsEnrollmentRecord(models.Model):
    Student_Users = models.ForeignKey(StudentProfile, related_name='+', on_delete=models.CASCADE,null=True)
    School_Year = models.ForeignKey(SchoolYear, related_name='+', on_delete=models.CASCADE, null=True, blank=True)
    Courses = models.ForeignKey(Course, related_name='+', on_delete=models.CASCADE, null=True, blank=True)

class StudentsSubmittedDocument(models.Model):
    Students_Enrollment_Records = models.ForeignKey(StudentsEnrollmentRecord, related_name='+', on_delete=models.CASCADE,blank=True,null=True)
    Document_Requirements = models.IntegerField(null=True,blank=True)
    Document = models.FileField(upload_to='files',null=True,blank=True)

如何解决此错误?我尝试了很多次来解决此问题,但我不能,我不知道如何解决此错误,我只想将StudentsEnrollmentRecord(Student_Users)保存到StudentsSubmittedDocument(Students_Enrollment_Records),这是我的错误:

ValueError at /newEnroll/ Cannot assign "": "StudentsSubmittedDocument.Students_Enrollment_Records" must be a "StudentsEnrollmentRecord" instance.

1 个答案:

答案 0 :(得分:1)

在变量insert_doc中,将Students_Enrollment_Records的参数从studentname更改为V_insert_data,如下所示:

insert_doc = StudentsSubmittedDocument(
    Students_Enrollment_Records = V_insert_data,
    Document = myfile
)

错误明确表明您没有为Students_Enrollment_Records模型中的StudentsSubmittedDocument字段分配正确的实例。

PD:我建议您更改ForeignKey字段的名称,并使其单数,因为它仅指向一条记录,而复数名称可能会导致混淆。 / p>