我有一个上传我的学期日期的API脚本。由于某些原因,当脚本运行时,我总是得到一个错误,即我有一个失败的外键约束。感谢任何关于如何解决这个问题的帮助。附件是我的模型和我的脚本。所有数据都不会保存到表中。感谢你们能给我的任何帮助。非常感谢!
models.py
# School Information Stored
class School(models.Model):
school_name = models.CharField(max_length = 50, default = "")
schoolpsid_id= models.CharField(default = "", max_length = 50, unique = True)
school_number = models.CharField(primary_key = True,default = "", max_length = 50, unique = True)
low_grade = models.CharField(default = "", max_length = 2 )
high_grade = models.CharField(default = "", max_length = 2 )
class Meta:
verbose_name = "School"
def __str__(self):
return self.school_name
# Term Information Stored
class Term(models.Model):
termpsid= models.CharField(default = "", max_length = 10, unique = True)
schoolpsid = models.ForeignKey(School,on_delete = models.CASCADE, default = "" ,)
start_year = models.CharField(max_length = 4, default = "")
portion = models.CharField(max_length = 10, default = "")
start_date = models.DateField(blank=True)
end_date = models.DateField(blank= True)
abbreviation = models.CharField(max_length = 50, default = "")
name = models.CharField(max_length = 50, default = "")
class Meta:
verbose_name = "Term"
def __str__(self):
return self.name powerschool.py
def ImportTerms(request):
print("Getting term data for K-8 (Creation-Update)")
#Pulls The K-8 Term Data
url = ""
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')
print("Analyzing term records for K-8")
soup = BeautifulSoup(encode_xml, 'lxml')
for term in soup.findAll('term'):
#XML Values
termpsid = term.find('id').text
print('---->%s' %termpsid)
# Updates Term Information If Exists
if Term.objects.filter(termpsid=termpsid).exists():
try:
termpsid = term.find('id').text
school_id = term.find('school_id').text
start_year = term.find('start_year').text
portion = term.find('portion').text
start_date = term.find('start_date').text
end_date = term.find('end_date').text
abbreviation = term.find('abbreviation').text
name = term.find('name').text
print('---->%s' %termpsid)
print('---->%s' %school_id)
print('---->%s' %start_year)
print('---->%s' %portion)
print('---->%s' %start_date)
print('---->%s' %end_date)
print('---->%s' %abbreviation)
print('---->%s' %name)
Term.objects(termpsid=termpsid).update(schoolpsid_id = school_id, start_year = start_year, portion = portion, start_date = start_date, end_date = end_date, abbreviation= abbreviation,
name = name)
except Exception as err1:
print ("Error at Term", str(err1))
else:
try:
termpsid = term.find('id').text
school_id = term.find('school_id').text
start_year = term.find('start_year').text
portion = term.find('portion').text
start_date = term.find('start_date').text
end_date = term.find('end_date').text
abbreviation = term.find('abbreviation').text
name = term.find('name').text
print('---->%s' %termpsid)
print('---->%s' %school_id)
print('---->%s' %start_year)
print('---->%s' %portion)
print('---->%s' %start_date)
print('---->%s' %end_date)
print('---->%s' %abbreviation)
print('---->%s' %name)
t = Term.objects.create(termpsid=termpsid,schoolpsid_id = school_id, start_year = start_year, portion = portion, start_date = start_date, end_date = end_date, abbreviation= abbreviation,
name = name)
t.save()
except Exception as err2:
print ("Error at Term", str(err2))
print("Getting term data for NHS School (Creation-Update)")
#Pulls The NHS Term Data
url = ""
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')
print("Analyzing term records for The Newmark School")
soup = BeautifulSoup(encode_xml, 'lxml')
for term in soup.findAll('term'):
#XML Values
termpsid = term.find('id').text
print('---->%s' %termpsid)
# Updates Term Information If Exists
if Term.objects.filter(termpsid=termpsid).exists():
try:
termpsid = term.find('id').text
school_id = term.find('school_id').text
start_year = term.find('start_year').text
portion = term.find('portion').text
start_date = term.find('start_date').text
end_date = term.find('end_date').text
abbreviation = term.find('abbreviation').text
name = term.find('name').text
print('---->%s' %termpsid)
print('---->%s' %school_id)
print('---->%s' %start_year)
print('---->%s' %portion)
print('---->%s' %start_date)
print('---->%s' %end_date)
print('---->%s' %abbreviation)
print('---->%s' %name)
Term.objects(termpsid=termpsid).update(schoolpsid_id = school_id, start_year = start_year, portion = portion, start_date = start_date, end_date = end_date, abbreviation= abbreviation,
name = name)
except Exception as err3:
print ("Error at Term", str(err3))
else:
try:
termpsid = term.find('id').text
school_id = term.find('school_id').text
start_year = term.find('start_year').text
portion = term.find('portion').text
start_date = term.find('start_date').text
end_date = term.find('end_date').text
abbreviation = term.find('abbreviation').text
name = term.find('name').text
print('---->%s' %termpsid)
print('---->%s' %school_id)
print('---->%s' %start_year)
print('---->%s' %portion)
print('---->%s' %start_date)
print('---->%s' %end_date)
print('---->%s' %abbreviation)
print('---->%s' %name)
t = Term.objects.create(termpsid=termpsid,schoolpsid_id = school_id, start_year = start_year, portion = portion, start_date = start_date, end_date = end_date, abbreviation= abbreviation,
name = name)
t.save()
except Exception as err4:
print ("Error at Term", str(err4)) 发布于 2020-09-18 22:44:49
我有能力解决这个问题。我必须在表中指定所有的默认主键。它不喜欢我引用其他不是主键的字段。
https://stackoverflow.com/questions/63945402
复制相似问题