Skip to main content
 首页 » 编程设计

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

2024年08月20日33leader

我正在使用 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)