第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

外鍵字段 Django 實例錯誤

外鍵字段 Django 實例錯誤

暮色呼如 2023-03-08 11:13:08
我很難過,需要有關我的功能的幫助。我有兩個表學生和學生信息。學生信息是該學生的所有監(jiān)護人信息。我將此數(shù)據(jù)與主學生表分開,因此您可以根據(jù)需要將任意數(shù)量的監(jiān)護人添加到具有新記錄的學生文件中。我得到的錯誤如下。無法分配“'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 ")
查看完整描述

1 回答

?
交互式愛情

TA貢獻1712條經(jīng)驗 獲得超3個贊

在使用外鍵關系創(chuàng)建記錄時,應提供相關表的實例,以便表可以維護該特定記錄的關系。


Student獲取具有給定的表的實例psid并在創(chuàng)建StudentInformation記錄時使用它


編輯mother:僅在和father值可用時包括用于創(chuàng)建記錄的部分。


for student in students:

      #XML Values

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

    try:

        psid_obj = Student.objects.get(studentpsid=psid) #(pk = psid) also works as the field is primary key

        try:

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

            m = StudentInformation.objects.create(studentpsid=psid_obj,guardian_name = mother, relation = "Mom")

            m.save()

        except Exception as err1:

            print "Error at Mom", str(err1)   

        try: 

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

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

            d.save()

        except Exception as err2:

            print "Error at Dad",str(err2)

    except:

        print "Student Record Not found"


查看完整回答
反對 回復 2023-03-08
  • 1 回答
  • 0 關注
  • 100 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號