Search code examples
pythondjangodjango-testing

django-money form field in unit test


I am using django-money and I have a money field (value = MoneyField(…)) I want to test in a model form. This is the code:

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

I get an error in the form parsing code stating:

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

What is the correct format?


Solution

  • django-money does a hack with their MoneyField, which doesn't translate to a simple HTML form field, and instead produces two HTML form fields for the value and the currency code.

    You have to pass value of type Decimal (or any value that can be coerced to Decimal) and value_currency of the 3-character currency code (ChoiceField of country codes).

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