gpt4 book ai didi

python - 单元测试中的 django-money 表单字段

转载 作者:太空宇宙 更新时间:2023-11-04 00:38:00 25 4
gpt4 key购买 nike

我正在使用 django-money我有一个货币字段 (value = MoneyField(...)) 我想以模型形式进行测试。这是代码:

def test_post_valid(self):
data = {'value': Money('99.99', currency='GBP'), }
response = self.client.post(url, data)

我在表单解析代码中得到一个错误:

(Pdb++) form.errors                                                             
{u'value': [u'This field is required.']}

什么是正确的格式?

最佳答案

django-money 对他们的 MoneyField 进行了破解,它不会转换为简单的 HTML 表单字段,而是为值和货币代码。

您必须传递 Decimal 类型的 value(或任何可以强制转换为 Decimal 的值)和 value_currency 三字符货币代码(ChoiceField 国家代码)。

def test_post_valid(self):
data = {'value_0': '99.99', 'value_1': 'GBP' }
response = self.client.post(url, data)

关于python - 单元测试中的 django-money 表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43068036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com