I'm want to test forms in my django app. I got a form with disabled field.
class SportPitchesForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(SportPitchesForm, self).__init__(*args, **kwargs)
self.fields["city"].disabled = True
self.fields['pitch'].required = False
This is my test
def test_sport_pitches_form(self):
form = SportPitchesForm(
data={
"name": "testpitch",
"city": 1,
"location": "ExamplePlace",
"location_lat": 40.00,
"location_lon": 50.45,
"pitch": [1, 2, 3],
}
)
print(form.errors)
self.assertTrue(form.is_valid())
I got this error:
<ul class="errorlist"><li>city<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
When field is enabled my test works.