Django 表达式只能用于更新,不能用于插入
我在尝试使用Factory boy和unittest.mock创建对象以模拟付款时遇到此问题Django 表达式只能用于更新,不能用于插入,django,pytest,factory-boy,vcr,python-unittest.mock,Django,Pytest,Factory Boy,Vcr,Python Unittest.mock,我在尝试使用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().resolv
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
self=,字段=
值=
def准备_值(自身、字段、值):
"""
通过解析查询中要使用的值(如果该值是
表达式,否则调用字段的get\u db\u prep\u save()。
"""
如果hasattr(值“解析表达式”):
value=value.resolve_表达式(self.query,allow_joins=False,for_save=True)
#不允许包含Col表达式的值。他们指的是
#行上的现有列,但在插入行的情况下
#还不存在。
如果value.com包含\u列\u引用:
升值误差(
'未能在%s上插入表达式“%s”。F()表达式'
>'只能用于更新,不能用于插入。“%”(值,字段)
)
E ValueError:未能在tap.TapSubscription.card_id.F()上插入表达式“”。表达式只能用于更新,不能用于插入。
/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py:1171:ValueError
这是我正在使用的导致问题的代码
@pytest.mark.django_db
@tap_vcr.use_cassette(match_on=('method', 'path'))
@override_settings(TAP_SECRET_KEY='test')
@mock.patch('core.payment.gateway.tap.utils.initiate_payment')
def test_subscription_handler(client, user):
user = UserFactory.create()
with mock.patch('core.payment.gateway.tap.utils.call_tap_api') as call_tap_api:
customer_id = create_customer(user)
card_token = tockenize_card('123456789',
'01',
'01',
'100',
user.username)
card_id = save_card(customer_id, card_token)
instance = TapSubscriptionFactory.create(user=user, card_id= card_id)
assert instance.id
create_customer()和tockenize_card()以及save_card()使用call_tap_api()函数,该函数用于调用支付api,使用@mock.patch。我得到的值类似于
我通过以下方式解决了这个问题:
@pytest.mark.django_db
@tap_vcr.use_cassette(match_on=('method', 'path'))
@override_settings(TAP_SECRET_KEY='test')
@mock.patch('core.payment.gateway.tap.utils.initiate_payment')
def test_subscription_handler(client, user):
user = UserFactory.create()
with mock.patch('core.payment.gateway.tap.utils.call_tap_api') as call_tap_api:
customer_id = user.tap_customer_id
card_token = factory.fuzzy.FuzzyInteger(1, 9999)
card_id = factory.fuzzy.FuzzyInteger(1, 9999)
instance = TapSubscriptionFactory.create(user=user, card_id= card_id)
assert instance.id
现在,customer_id和card_token以及card_id将获得生成的值,而不是
<MagicMock name='call_tap_api().get().resolve_expression()' id='140651554367360'>
这是我正在使用的导致问题的代码
@pytest.mark.django_db
@tap_vcr.use_cassette(match_on=('method', 'path'))
@override_settings(TAP_SECRET_KEY='test')
@mock.patch('core.payment.gateway.tap.utils.initiate_payment')
def test_subscription_handler(client, user):
user = UserFactory.create()
with mock.patch('core.payment.gateway.tap.utils.call_tap_api') as call_tap_api:
customer_id = create_customer(user)
card_token = tockenize_card('123456789',
'01',
'01',
'100',
user.username)
card_id = save_card(customer_id, card_token)
instance = TapSubscriptionFactory.create(user=user, card_id= card_id)
assert instance.id
create_customer()和tockenize_card()以及save_card()使用call_tap_api()函数,该函数用于调用支付api,使用@mock.patch。我得到的值类似于
我通过以下方式解决了这个问题:
@pytest.mark.django_db
@tap_vcr.use_cassette(match_on=('method', 'path'))
@override_settings(TAP_SECRET_KEY='test')
@mock.patch('core.payment.gateway.tap.utils.initiate_payment')
def test_subscription_handler(client, user):
user = UserFactory.create()
with mock.patch('core.payment.gateway.tap.utils.call_tap_api') as call_tap_api:
customer_id = user.tap_customer_id
card_token = factory.fuzzy.FuzzyInteger(1, 9999)
card_id = factory.fuzzy.FuzzyInteger(1, 9999)
instance = TapSubscriptionFactory.create(user=user, card_id= card_id)
assert instance.id
现在,customer_id和card_token以及card_id将获得生成的值,而不是
<MagicMock name='call_tap_api().get().resolve_expression()' id='140651554367360'>
嘿!没有模型、工厂和模拟的代码,很难了解发生了什么;)如果你能提供其中的一部分——也许是使用MagicMock的部分——这将有助于提供一个合适的asnswer!嘿没有模型、工厂和模拟的代码,很难了解发生了什么;)如果你能提供其中的一部分——也许是使用MagicMock的部分——这将有助于提供一个合适的asnswer!