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

标签 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") 时,而不是更新字符串中提供的字段,它查找具有字符串中每个单独字符的字段。

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

最佳答案

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

The update_fields argument can be any iterable containing strings. An empty update_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/

相关文章:

python - 替换 Python 中的非 ASCII 字符 : eg, ' vs. ’

python - numpy.array bool 值与 int 的混合

python - django中的url可以传入空字符串吗?

python - Django 授权 - 在函数调用中返回重定向

python - 如何从 App Engine (Python) Datastore 中获取当月的记录?

python - Python/C API 中 Python 胶囊的安全和防御编码视角

python - 如何在 Django 中反转多个级别的查询对象?

python - Django ForeignKey 哪个不需要参照完整性?

django - python的datetime.date减去django.db.models.DateField得到年龄

django - 关于 Django 中 OneToOne 模型的哲学问题