Django unit test formset

Django unit test formset

When testing page that contains formset in Django using unit test, we may encounter :

1
ValidationError: [u'ManagementForm data is missing or has been tampered with']

This is happend because we POST data without required FORMSET hidden value in form. To solve this issue, inspect element on your formset and insert hidden value in payload data.

Example :

1
2
<input id="id_firstformaset-MIN_NUM_FORMS" name="firstformaset-MIN_NUM_FORMS" type="hidden" value="0">
...

And put this in our payload:

1
2
3
4
5
6
7
8
        self.correct = {
            'name': 'TESTDATA',
            'gender': self.model.FEMALE_STATUS,
            'firstformaset-TOTAL_FORMS': 1,
            'firstformaset-INITIAL_FORMS': 0,
            'firstformaset-MIN_NUM_FORMS': 0,
            'firstformaset-MAX_NUM_FORMS': 1,
        }

One Reply to “Django unit test formset”

Leave a Reply

Your email address will not be published. Required fields are marked *