Django .save() 不可预测地处理 update_fields 输入

Ada*_*rrh 3 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
Run Code Online (Sandbox Code Playgroud)

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

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

ale*_*cxe 5

update_fields 应该是一个包含字段名称的可迭代对象:

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

self.save(update_fields=["rate_usd_inr"])
Run Code Online (Sandbox Code Playgroud)