Django .save()无法预测处理update_fields输入

时间:2016-03-05 17:12:05

标签: python django django-models

追溯讲述了这个故事:

Error
Traceback (most recent call last):
  File "/Users/adamstarrh/almondking/AlmondKing/tests/test_models/test_financial_logs.py", line 35, in test_cogs_per_tag
    self.assertEqual(self.sale5.cogs_per_tag, {10: 1813365, 3: 5623801, 4: 4140737})
  File "/Users/adamstarrh/almondking/AlmondKing/FinancialLogs/models.py", line 244, in cogs_per_tag
    cogs[tag[0]] = float(round(shipment.adjusted_cost * tag[1]))
  File "/Users/adamstarrh/almondking/AlmondKing/InventoryLogs/models.py", line 509, in adjusted_cost
    return self.cost_inr_per_kg
  File "/Users/adamstarrh/almondking/AlmondKing/InventoryLogs/models.py", line 499, in cost_inr_per_kg
    Decimal(self.reverse_exchange_rate), 4)
  File "/Users/adamstarrh/almondking/AlmondKing/InventoryLogs/models.py", line 477, in reverse_exchange_rate
    self.save(update_fields="rate_usd_inr")
  File "/Users/adamstarrh/almondking/AlmondKing/lib/python3.5/site-packages/django/db/models/base.py", line 714, in save
    % ', '.join(non_model_fields))
ValueError: The following fields do not exist in this model or are m2m fields: u, _, n, a, r, s, e, t, i, d

当我尝试调用self.save(update_fields="rate_usd_inr")时,它不会更新字符串中提供的字段,而是查找字符串中每个字符的字段。

这对我来说已经好几个月了。我很确定我昨天停止工作时成功运行了测试。在我编写代码时,我当然从未遇到过它。它刚刚开始出现,当我今天早上再次运行它们时。

1 个答案:

答案 0 :(得分:4)

update_fields应该是一个可迭代的字段名称:

  

update_fields参数可以是包含字符串的任何可迭代参数。一个   空update_fields iterable将跳过保存。值为None   对所有字段执行更新。

self.save(update_fields=["rate_usd_inr"])