我正在尝试使用update_or_create来更新模型的字段,该方法是从api获取数据的。问题是所有字段都只填充了i ['Concessional]和i ['Noncessional']
中的最后两个值我正在使用外部api更新以下模型:
class Contributions(models.Model):
trustee = models.ForeignKey(Trustee, on_delete = models.CASCADE)
concessional_caps = models.FloatField(null = True, blank = True)
noncessional_caps = models.FloatField(null = True, blank = True)
concessional_contributions_to_this_fund = models.FloatField(null = True, blank = True)
non_concessional_contributions_to_this_fund = models.FloatField(null = True, blank = True)
concessional_contributions_to_other_fund = models.FloatField(null = True, blank = True)
non_concessional_contributions_to_other_fund = models.FloatField(null = True, blank = True)
concessional_contributions_as_allocated =models.FloatField(null = True, blank = True)
non_concessional_contributions_as_allocated = models.FloatField(null = True, blank = True)
concessional_amounts_above_caps = models.FloatField(null = True, blank = True)
non_concessional_amounts_above_caps = models.FloatField(null = True, blank = True)
concessional_available_total = models.FloatField(null = True, blank = True)
non_concessional_available_total = models.FloatField(null = True, blank = True)
使用以下代码:
for i in range(len(contribution_caps_data['Data']['Members'])):
Contributions.objects.update_or_create(trustee = Trustee.objects.get(trustee_name = contribution_caps_data['Data']['Members'][i]["Name"]))
for j in contribution_caps_data['Data']['Members'][i]['YearBreakdown']['ContributionDetails']:
Contributions.objects.update_or_create(trustee = Trustee.objects.get(trustee_name = contribution_caps_data['Data']['Members'][i]["Name"]),
defaults = {
'concessional_caps': j['Concessional'],
'noncessional_caps': j['NonConcessional'],
'concessional_contributions_to_this_fund': j['Concessional'],
'non_concessional_contributions_to_this_fund': j['NonConcessional'],
'concessional_contributions_to_other_fund': j['Concessional'],
'non_concessional_contributions_as_allocated': j['NonConcessional'],
'concessional_contributions_as_allocated': j['Concessional'],
'non_concessional_amounts_above_caps': j['NonConcessional'],
'concessional_amounts_above_caps': j['Concessional'],
'non_concessional_amounts_above_caps': j['NonConcessional'],
'concessional_available_total': j['Concessional'],
'non_concessional_available_total': j['NonConcessional']
}
)
我正在尝试创建新的Contributions记录,第一个循环是获取名称(外键),第二个循环是实际获取贡献的值
您如何建议解决此问题。我应该先预处理成可用形式(可能是字典),然后更新模型。原谅我这是一个微不足道的问题,但是我对Python和Django还是比较陌生,但是我似乎无法找出解决方案。
答案 0 :(得分:0)
trustee_name
获得实例?人们可以使用相同的名字... range(len(...
从我的头顶上方,这会更好
for member in contribution_caps_data['Data']['Members']:
trustee, created = Trustee.objects.get_or_create(trustee_name=member['Name'])
if created:
trustee.save()
for contribution_detail in member['YearBreakdown']['ContributionDetails']:
# WARNING: you need to change this, otherwise you'll always get the same contribution for the trustee...
# Perhaps there's the year in the YearBreakdown...
contribution, created = Contributions.objects.get_or_create(trustee=trustee) # gets same contribution in each iteration...
contribution.concessional_caps = contribution_detail['Concessional']
contribution.noncessional_caps = contribution_detail['NonConcessional']
# ... the same for all other attributes ...
contribution.save()