追溯讲述了这个故事:
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")
时,而不是更新字符串中提供的字段,它查找具有字符串中每个单独字符的字段。
几个月来这对我来说一直很好用。我很确定我昨天停止工作时成功地运行了测试。我在编写代码时当然从未遇到过它。今天早上我再次运行它们时,它才开始出现。
最佳答案
update_fields
应该是一个包含字段名的可迭代对象:
The
update_fields
argument can be any iterable containing strings. An emptyupdate_fields
iterable will skip the save. A value of None will perform an update on all fields.
self.save(update_fields=["rate_usd_inr"])
关于python - Django .save() 不可预测地处理 update_fields 输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35817126/