Django:update_or_create重复值被填充

时间:2019-03-15 00:28:36

标签: python django

我正在尝试使用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还是比较陌生,但是我似乎无法找出解决方案。

1 个答案:

答案 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()