[Django] #27434: Crashes during form validation when Model validation raises ValidationErrors for fields not in the current form

12 views
Skip to first unread message

Django

unread,
Nov 4, 2016, 9:44:48 AM11/4/16
to django-...@googlegroups.com
#27434: Crashes during form validation when Model validation raises
ValidationErrors for fields not in the current form
-----------------------------------------------+------------------------
Reporter: Matthias Kestenholz | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.10
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------------+------------------------
Example:

{{{
class DictionaryValidationErrorModel(models.Model):
field1 = models.CharField(max_length=100)
field2 = models.CharField(max_length=100)
field3 = models.CharField(max_length=100)

def clean(self):
super(DictionaryValidationErrorModel, self).clean()
raise ValidationError({
'field1': 'field1 error',
'field2': 'field2 error',
})
}}}

{{{
class DictionaryValidationErrorTests(ValidationTestCase):
def test_default_form(self):
class ModelForm(forms.ModelForm):
class Meta:
model = DictionaryValidationErrorModel
fields = ('field1', 'field2', 'field3')

form = ModelForm({
'field1': '1',
'field2': '2',
'field3': '3',
})
self.assertFalse(form.is_valid())
self.assertEqual(set(form.errors.keys()), {'field1', 'field2'})

def test_crash(self):
class ModelForm(forms.ModelForm):
class Meta:
model = DictionaryValidationErrorModel
fields = ('field1', 'field3')

form = ModelForm({
'field1': '1',
'field2': '2',
'field3': '3',
})
self.assertFalse(form.is_valid())
self.assertEqual(set(form.errors.keys()), {'field1', 'field2'})
}}}

The problem I'm seeing is
{{{

======================================================================
ERROR: test_crash
(validation.test_validators.DictionaryValidationErrorTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python3.5/unittest/case.py", line 59, in testPartExecutor
yield
File "/usr/lib/python3.5/unittest/case.py", line 601, in run
testMethod()
File
"/home/matthias/Projects/django/tests/validation/test_validators.py", line
62, in test_crash
self.assertFalse(form.is_valid())
File "/home/matthias/Projects/django/django/forms/forms.py", line 169,
in is_valid
return self.is_bound and not self.errors
File "/home/matthias/Projects/django/django/forms/forms.py", line 161,
in errors
self.full_clean()
File "/home/matthias/Projects/django/django/forms/forms.py", line 372,
in full_clean
self._post_clean()
File "/home/matthias/Projects/django/django/forms/models.py", line 400,
in _post_clean
self._update_errors(e)
File "/home/matthias/Projects/django/django/forms/models.py", line 374,
in _update_errors
self.add_error(None, errors)
File "/home/matthias/Projects/django/django/forms/forms.py", line 338,
in add_error
"'%s' has no field named '%s'." % (self.__class__.__name__, field))
ValueError: 'ModelForm' has no field named 'field2'.
}}}

This might be a thing for me to work on during the sprints. Also,
https://github.com/matthiask/django/commit/2d03c38f888262e63c179b57c0c6bd09312f6f99

(Cc:ing Loïc because we discussed this at DUTH on wednesday evening.)

--
Ticket URL: <https://code.djangoproject.com/ticket/27434>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 5, 2016, 6:36:21 AM11/5/16
to django-...@googlegroups.com
#27434: Crashes during form validation when Model validation raises
ValidationErrors for fields not in the current form
-------------------------------------+-------------------------------------
Reporter: Matthias Kestenholz | Owner: Matthias
| Kestenholz
Type: Bug | Status: assigned
Component: Forms | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Matthias Kestenholz):

* owner: nobody => Matthias Kestenholz
* status: new => assigned
* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/27434#comment:1>

Django

unread,
Nov 5, 2016, 6:46:52 AM11/5/16
to django-...@googlegroups.com
#27434: Crashes during form validation when Model validation raises
ValidationErrors for fields not in the current form
-------------------------------------+-------------------------------------
Reporter: Matthias Kestenholz | Owner: Matthias
| Kestenholz
Type: Bug | Status: assigned
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* version: 1.10 => master
* stage: Unreviewed => Accepted


Comment:

I'm still not sure if we should discard the error or move it to
`NON_FIELD_ERRORS` as suggested here but it shouldn't be crashing.

[https://github.com/django/django/pull/7473 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/27434#comment:2>

Django

unread,
Nov 15, 2016, 11:41:15 AM11/15/16
to django-...@googlegroups.com
#27434: Crashes during form validation when Model validation raises
ValidationErrors for fields not in the current form
-------------------------------------+-------------------------------------
Reporter: Matthias Kestenholz | Owner: Matthias
| Kestenholz
Type: Bug | Status: assigned
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/27434#comment:3>

Django

unread,
Jan 4, 2017, 8:44:58 AM1/4/17
to django-...@googlegroups.com
#27434: Crashes during form validation when Model validation raises
ValidationErrors for fields not in the current form
-------------------------------------+-------------------------------------
Reporter: Matthias Kestenholz | Owner: Matthias
| Kestenholz
Type: Bug | Status: assigned
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Matthias Kestenholz):

* needs_better_patch: 1 => 0


Comment:

I hope the patch won't need further improvement, removing the flag for
now. Thanks.

--
Ticket URL: <https://code.djangoproject.com/ticket/27434#comment:4>

Django

unread,
Apr 23, 2017, 6:58:24 AM4/23/17
to django-...@googlegroups.com
#27434: Crashes during form validation when Model validation raises
ValidationErrors for fields not in the current form
-------------------------------------+-------------------------------------
Reporter: Matthias Kestenholz | Owner: Matthias
| Kestenholz
Type: Bug | Status: assigned
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Matthias Kestenholz):

New pull request containing only a documentation patch:

[https://github.com/django/django/pull/8394 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/27434#comment:5>

Django

unread,
Apr 30, 2017, 3:57:59 AM4/30/17
to django-...@googlegroups.com
#27434: Crashes during form validation when Model validation raises
ValidationErrors for fields not in the current form
-------------------------------------+-------------------------------------
Reporter: Matthias Kestenholz | Owner: Matthias
| Kestenholz
Type: Bug | Status: assigned
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Matthias Kestenholz):

* stage: Accepted => Ready for checkin


Comment:

IMO this is ready.

--
Ticket URL: <https://code.djangoproject.com/ticket/27434#comment:6>

Django

unread,
May 25, 2017, 8:18:48 PM5/25/17
to django-...@googlegroups.com
#27434: Document caveats of raising a ValidationError in Model.clean() for a field
not in a model form

-------------------------------------+-------------------------------------
Reporter: Matthias Kestenholz | Owner: Matthias
Type: | Kestenholz
Cleanup/optimization | Status: assigned
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* component: Forms => Documentation


* needs_better_patch: 0 => 1

* stage: Ready for checkin => Accepted
* type: Bug => Cleanup/optimization


Comment:

I left a few ideas for improvement on the PR.

--
Ticket URL: <https://code.djangoproject.com/ticket/27434#comment:7>

Django

unread,
Jun 13, 2017, 8:47:50 PM6/13/17
to django-...@googlegroups.com
#27434: Document caveats of raising a ValidationError in Model.clean() for a field
not in a model form
-------------------------------------+-------------------------------------
Reporter: Matthias Kestenholz | Owner: Matthias
Type: | Kestenholz
Cleanup/optimization | Status: closed
Component: Documentation | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"e8c056c31a5b353e7b50a405c00db12c28f4a756" e8c056c]:
{{{
#!CommitTicketReference repository=""
revision="e8c056c31a5b353e7b50a405c00db12c28f4a756"
Fixed #27434 -- Doc'd how to raise a model validation error for a field
not in a model form.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27434#comment:8>

Django

unread,
Jun 13, 2017, 8:48:16 PM6/13/17
to django-...@googlegroups.com
#27434: Document caveats of raising a ValidationError in Model.clean() for a field
not in a model form
-------------------------------------+-------------------------------------
Reporter: Matthias Kestenholz | Owner: Matthias
Type: | Kestenholz
Cleanup/optimization | Status: closed
Component: Documentation | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"a1c6c220e2ac86a74869d0017cd658115cc4ad7b" a1c6c220]:
{{{
#!CommitTicketReference repository=""
revision="a1c6c220e2ac86a74869d0017cd658115cc4ad7b"
[1.11.x] Fixed #27434 -- Doc'd how to raise a model validation error for a


field not in a model form.

Backport of e8c056c31a5b353e7b50a405c00db12c28f4a756 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27434#comment:9>

Reply all
Reply to author
Forward
0 new messages