1 回答
TA贡献1712条经验 获得超3个赞
在使用外键关系创建记录时,应提供相关表的实例,以便表可以维护该特定记录的关系。
Student获取具有给定的表的实例psid并在创建StudentInformation记录时使用它
编辑mother:仅在和father值可用时包括用于创建记录的部分。
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"
添加回答
举报