在Django中测试formsets

时间:2015-07-31 15:58:56

标签: django forms unit-testing formsets

如何使用客户端self.client.post('/url/', {})在单元测试中传递formset的值?我收到验证错误,说管理层已被篡改。

提前致谢!

1 个答案:

答案 0 :(得分:0)

这是获取用户技能的示例formset

class SkillForm(forms.Form):
    level_id = forms.ChoiceField(choices=LEVEL_CHOICES, required = True)
    txt_skills = forms.CharField(max_length=250, required = True)

SkillFormset = formset_factory(SkillForm,extra=1, max_num=10)

这是测试用例的示例POST数据。

self.post_data.update({
        'skillform-0-level_id': '2',
        'skillform-0-txt_skills': 'Python',
        'skillform-1-level_id': '3',
        'skillform-1-txt_skills': 'Java',
        'skillform-TOTAL_FORMS': '2',
        'skillform-INITIAL_FORMS': '1',
        'skillform-MAX_NUM_FORMS': '10'
        })