【发布时间】:2020-07-03 18:32:21
【问题描述】:
我在尝试使用 Factory boy 和 unittest.mock 创建对象来模拟付款时遇到了这个问题
self = <django.db.models.sql.compiler.SQLInsertCompiler object at 0x7f5aa2913400>, field = <django.db.models.fields.CharField: card_id>
value = <MagicMock name='call_tap_api().get().resolve_expression()' id='140027256369728'>
def prepare_value(self, field, value):
"""
Prepare a value to be used in a query by resolving it if it is an
expression and otherwise calling the field's get_db_prep_save().
"""
if hasattr(value, 'resolve_expression'):
value = value.resolve_expression(self.query, allow_joins=False, for_save=True)
# Don't allow values containing Col expressions. They refer to
# existing columns on a row, but in the case of insert the row
# doesn't exist yet.
if value.contains_column_references:
raise ValueError(
'Failed to insert expression "%s" on %s. F() expressions '
> 'can only be used to update, not to insert.' % (value, field)
)
E ValueError: Failed to insert expression "<MagicMock name='call_tap_api().get().resolve_expression()' id='140027256369728'>" on tap.TapSubscription.card_id. F() expressions can only be used to update, not to insert.
/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py:1171: ValueError
【问题讨论】:
-
嘿!如果没有模型、工厂和模拟的代码,很难了解正在发生的事情;)如果您能够提供其中的一部分——也许是使用 MagicMock 的部分——这将有助于提供一个正确的答案!
标签: django pytest factory-boy vcr python-unittest.mock