Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class IncidentEdit(UpdateView):
- model=Incident
- fields = visible_field_list
- sucess_url = '/status'
- class IncidentForm(ModelForm):
- class Meta:
- model = Incident
- # Define fields you want here, it is best practice not to use '__all__'
- fields = [...]
- def clean(self):
- cleaned_data = super(IncidentForm, self).clean()
- field_1 = cleaned_data.get('field_1')
- field_2 = cleaned_data.get('field_2')
- field_3 = cleaned_data.get('field_3')
- # Values may be None if the fields did not pass previous validations.
- if field_1 is not None and field_2 is not None and field_3 is not None:
- # If fields have values, perform validation:
- if not field_3 == field_1 + field_2:
- # Use None as the first parameter to make it a non-field error.
- # If you feel is related to a field, use this field's name.
- self.add_error(None, ValidationError('field_3 must be equal to the sum of field_1 and filed_2'))
- # Required only if Django version < 1.7 :
- return cleaned_data
- class IncidentEdit(UpdateView):
- model = Incident
- form_class = IncidentForm
- fields = visible_field_list
- success_url = '/status'
- class IncidentEdit(UpdateView):
- ...
- def form_valid(self, form):
- if form.cleaned_data['email'] in
- [i.email for i in Incident.objects.exclude(id=get_object().id)]:
- # Assume incident have email and it should be unique !!
- form.add_error('email', 'Incident with this email already exist')
- return self.form_invalid(form)
- return super(IncidentEdit, self).form_valid(form)
- # models.py
- class Incident(models.Model):
- numboys = models.SmallIntegerField(default=0)
- numgirls = models.SmallIntegerField(default=0)
- classttl = models.SmallIntegerField(default=0)
- # views.py
- def retunTestPassedResp(request):
- return HttpResponse()
- class NumValidationMixin:
- def form_valid(self, form):
- data = self.request.POST
- boys = data.get('numboys')
- girls = data.get('numgirls')
- ttl = data.get('classttl')
- if boys and girls and ttl:
- if int(ttl) == int(boys) + int(girls):
- return super().form_valid(form)
- # use form.errors to add the error msg as a dictonary
- form.errors['input invalid'] = '%s + %s not equal %s'%(boys, girls, ttl)
- form.errors['input invalid'] = 'losing input with boys or other'
- return self.form_invalid(form)
- class UpdateIncident(NumValidationMixin, UpdateView):
- model = Incident
- fields = ['numboys', 'numgirls', 'classttl']
- success_url = reverse_lazy('test-passed')
- # templates/.../Incident_form.html
- [...]
- <body>
- {{form}}
- {% if form.errors %}
- <p>get error</p>
- {{form.errors}}
- {% endif %}
- </body>
- # tests.py
- class IncidentUpdateTest(TestCase):
- def setUp(self):
- Incident.objects.create()
- def test_can_update_with_right_data(self):
- [...]
- def test_invalid_error_with_illegal_post(self):
- response = self.client.post(
- reverse('update-incident', args=(1,)),
- data={'numboys': '1', 'numgirls': '1', 'classttl': '3'}
- )
- self.assertEqual(Incident.objects.first().classttl, 0)
- # testing response page showing error msg
- self.assertContains(response, 'not equal')
Add Comment
Please, Sign In to add comment