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"
添加回答
舉報