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

django-money form field in unit test

我正在使用 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 的任何值)和 3 字符货币代码的 value_currency ( ChoiceField 个国家/地区代码)。

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