Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用测试客户端和post方法使用ModelChoiceField测试Django表单_Python_Django_Django Forms_Django Testing_Django Tests - Fatal编程技术网

Python 如何使用测试客户端和post方法使用ModelChoiceField测试Django表单

Python 如何使用测试客户端和post方法使用ModelChoiceField测试Django表单,python,django,django-forms,django-testing,django-tests,Python,Django,Django Forms,Django Testing,Django Tests,如何使用Django test client.post测试具有ModelChoiceField的表单?传递给post方法的数据字典应该如何编写?我的做法根本没有选择任何值 我有一个带有以下字段的表单: country = forms.ModelChoiceField( label="País", queryset=Country.objects.all().order_by('name'), required=True, widge

如何使用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)和国家名称(“巴西”)或其他信息。

我想您需要传递国家的ID或模型实例

如果您有一个id为32的国家/地区“巴西”,则可以输入

{....
    'country' : 32
....}

你可以先使用

country = Country.objects.get(id=32)
{....
    'country': country
....}