如何使用 Django test client.post 来测试具有 ModelChoiceField 的表单?传递给post方法的数据字典应该怎么写呢?我所做的方式根本不选择任何值。
我有一个包含以下字段的表单:
country = forms.ModelChoiceField(
label="País",
queryset=Country.objects.all().order_by('name'),
required=True,
widget=forms.Select(attrs={
'onchange': "Dajaxice.party.update_country(Dajax.process, {'option':this.value})"
},
)
我还有以下测试用例:
def test_party_profile_sucessfully_saved(self):
self.client.login(username='Party1', password='BadMotherF')
response = self.client.post(reverse('party'), data={'slx_legal_type': '1', 'city': 'Belo Horizonte', 'country': '32',
'mobile': '+55-31-55555555', 'name': 'Roberto Vasconcelos Novaes',
'phone': '+55-31-55555555', 'slx_cnpj': '', 'slx_cpf': '056846515',
'slx_ie': '', 'slx_im': '', 'slx_rg': 'MG9084545', 'street':
'Rua Palmira, 656 - 502', 'streetbis': 'Serra', 'subdivision': '520',
'zip': '30220110'},
follow=True)
self.assertContains(response, 'Succesfully Saved!')
这个表格可以正常工作。但是,当我使用上述测试用例对其进行测试时,作为模型选择字段(国家/地区)的数据传递的选择不会被选择。我尝试传递值(32)和国家/地区名称(“巴西”)或其他名称。