gpt4 book ai didi

python - Django:update_or_create重复值被填充

转载 作者:太空宇宙 更新时间:2023-11-03 21:09:17 26 4
gpt4 key购买 nike

我正在尝试使用 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']
}

)

我正在尝试创建一个新的贡献记录,第一个循环是获取名称(外键),第二个循环是实际获取贡献值

您建议如何解决这个问题。我应该首先预处理成可用的形式(也许是字典),然后更新模型。请原谅我这是一个微不足道的问题,但我对 Python 和 Django 比较陌生,但我似乎无法找出解决方案。

最佳答案

  • 您从未保存过数据
  • 模型受托人: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()

关于python - Django:update_or_create重复值被填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55173925/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com