2
如何使用Django測試client.post來測試具有ModelChoiceField的表單?應該如何將數據字典傳遞給post方法?我所做的事情根本沒有選擇任何價值。如何使用測試客戶端和Post方法使用ModelChoiceField測試Django表單
我有一個具有以下字段的表單:
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)和國家的名稱('巴西')或其他。