2014-01-10 2 views
2

Django 테스트 client.post를 사용하여 ModelChoiceField가있는 폼을 테스트하려면 어떻게해야합니까? post 메서드에 전달 된 데이터 사전은 어떻게 작성해야합니까? 내가하는 방식은 전혀 가치를 선택하지 않습니다.테스트 클라이언트 및 포스트 메소드를 사용하여 ModelChoiceField로 장고 폼을 테스트하는 방법

나는 다음 필드와 양식을 가지고 :

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)과 나라 이름 ('Brasil') 또는 무엇이든을 전달하려고 시도했다.

답변

1

국가 또는 모델 인스턴스의 ID를 전달해야합니다. 당신이 ID를 32 나라 '브라질'이있는 경우는

country = Country.objects.get(id=32) 
{.... 
    'country': country 
....} 
를 사용하여

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

또는

먼저 나라를 얻을 수에

전달할 수 있습니다

관련 문제