外键字段 Django 实例错误

我很难过,需要有关我的功能的帮助。我有两个表学生和学生信息。学生信息是该学生的所有监护人信息。我将此数据与主学生表分开,因此您可以根据需要将任意数量的监护人添加到具有新记录的学生文件中。我得到的错误如下。


无法分配“'1'”:“StudentInformation.studentpsid”必须是“Student”实例。


附件你会看到我的代码。学生信息中的studentpsid是student的外键。


def ImportStudentGuardian(request):

    AuthTokenP(request)

    print("Getting student guardian data from SIS for K-8")

    #Pulls K-8 Guardians

    url = "removed for posting"

    payload = {}

    token = APIInformation.objects.get(api_name="PowerSchool")

    key = token.key

    headers = {'Authorization': 'Bearer {}'.format(key)}   

    response = requests.request("GET", url, headers=headers, data = payload)

    encode_xml = response.text.encode('utf8')

    xml_string = ET.fromstring(encode_xml)

    students = xml_string.findall("student")

    for student in students:

      #XML Values

      psid = student.find("id").text

      try:

       mother = student.find("contact").find("mother").text

      except Exception:

       mother = ""   

      try: 

       father = student.find("contact").find("father").text

      except Exception:

       father = "" 

      if Student.objects.filter(studentpsid=psid).exists():  

        print("Accessing guardian information.")

        m = StudentInformation.objects.create(studentpsid=psid,guardian_name = mother, relation = "Mom")   <---- Function Fails here

        print("Record doesn't exist for mom, creating record.")

        m.save()

        d= StudentInformation.objects.create(studentpsid=psid,guardian_name = father, relation = "Dad")

        print("Record doesn't exist for dad, creating record.")

        d.save()

      return ("Updated Guardian Information ")


暮色呼如
浏览 89回答 1
1回答

交互式爱情

在使用外键关系创建记录时,应提供相关表的实例,以便表可以维护该特定记录的关系。Student获取具有给定的表的实例psid并在创建StudentInformation记录时使用它编辑mother:仅在和father值可用时包括用于创建记录的部分。for student in students:&nbsp; &nbsp; &nbsp; #XML Values&nbsp; &nbsp; psid = student.find("id").text&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; psid_obj = Student.objects.get(studentpsid=psid) #(pk = psid) also works as the field is primary key&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mother = student.find("contact").find("mother").text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m = StudentInformation.objects.create(studentpsid=psid_obj,guardian_name = mother, relation = "Mom")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m.save()&nbsp; &nbsp; &nbsp; &nbsp; except Exception as err1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print "Error at Mom", str(err1)&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; father = student.find("contact").find("father").text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d= StudentInformation.objects.create(studentpsid=psid_obj,guardian_name = father, relation = "Dad")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d.save()&nbsp; &nbsp; &nbsp; &nbsp; except Exception as err2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print "Error at Dad",str(err2)&nbsp; &nbsp; except:&nbsp; &nbsp; &nbsp; &nbsp; print "Student Record Not found"
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python