[Django] #17413: Serialization or getting non-HTML version of form errors is not easy

12 views
Skip to first unread message

Django

unread,
Dec 15, 2011, 8:23:33 AM12/15/11
to django-...@googlegroups.com
#17413: Serialization or getting non-HTML version of form errors is not easy
-----------------------------+---------------------------------------
Reporter: kmike | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version:
Severity: Normal | Keywords: form errors serialization
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+---------------------------------------
See e.g. http://forrst.com/posts/Convert_Django_form_errors_to_JSON-ijw

What do you think about adding errors_dict attribute to BaseForm? A rough
draft:

{{{
def _get_errors_dict(self):
return dict(
(k, map(unicode, v))
for (k,v) in self.errors.iteritems()
)
errors_dict = property(_get_errors_dict)
}}}

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

Django

unread,
Dec 24, 2011, 10:25:25 AM12/24/11
to django-...@googlegroups.com
#17413: Serialization or getting non-HTML version of form errors is not easy
-------------------------------------+-------------------------------------
Reporter: kmike | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version:
Severity: Normal | Resolution:
Keywords: form errors | Triage Stage: Design
serialization | decision needed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by aaugustin):

* needs_better_patch: => 0
* stage: Unreviewed => Design decision needed
* needs_tests: => 0
* needs_docs: => 0


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

Django

unread,
Mar 23, 2013, 11:32:34 AM3/23/13
to django-...@googlegroups.com
#17413: Serialization or getting non-HTML version of form errors is not easy
-------------------------------------+-------------------------------------

Reporter: kmike | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version:
Severity: Normal | Resolution:
Keywords: form errors | Triage Stage: Accepted
serialization | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by apollo13):

* stage: Design decision needed => Accepted


Comment:

Would be nice to have.

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

Django

unread,
Jul 28, 2013, 11:40:35 AM7/28/13
to django-...@googlegroups.com
#17413: Serialization or getting non-HTML version of form errors is not easy
-------------------------------------+-------------------------------------

Reporter: kmike | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version:
Severity: Normal | Resolution:
Keywords: form errors | Triage Stage: Accepted
serialization | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by merb):

Wouldn't it be better to rewrite

{{{
class ErrorDict(dict)
}}}

and add as_json or as_dict?
Currently it has as_text and as_ul

This is how the ErrorDict got returned, so a simple

{{{
@property
def errors(self):
"Returns an ErrorDict for the data provided for the form"
if self._errors is None:
self.full_clean()
return self._errors
}}}

The next snippet would call the json.dumps of the error dict.
{{{
print json.dumps(form.errors.as_dict())
}}}
I apped a patch that would do the following. If this is wished we only
need a small test.

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

Django

unread,
Jul 28, 2013, 11:42:20 AM7/28/13
to django-...@googlegroups.com
#17413: Serialization or getting non-HTML version of form errors is not easy
-------------------------------------+-------------------------------------
Reporter: kmike | Owner: merb
Type: New feature | Status: assigned
Component: Forms | Version:

Severity: Normal | Resolution:
Keywords: form errors | Triage Stage: Accepted
serialization | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by merb):

* owner: nobody => merb
* status: new => assigned


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

Django

unread,
Jul 28, 2013, 11:51:22 AM7/28/13
to django-...@googlegroups.com
#17413: Serialization or getting non-HTML version of form errors is not easy
-------------------------------------+-------------------------------------
Reporter: kmike | Owner: merb
Type: New feature | Status: assigned
Component: Forms | Version:

Severity: Normal | Resolution:
Keywords: form errors | Triage Stage: Accepted
serialization | Needs documentation: 1
Has patch: 1 | Patch needs improvement: 0
Needs tests: 1 | UI/UX: 0
Easy pickings: 1 |
-------------------------------------+-------------------------------------
Changes (by merb):

* needs_docs: 0 => 1
* has_patch: 0 => 1
* needs_tests: 0 => 1
* easy: 0 => 1


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

Django

unread,
Jul 29, 2013, 6:08:56 AM7/29/13
to django-...@googlegroups.com
#17413: Serialization or getting non-HTML version of form errors is not easy
-------------------------------------+-------------------------------------
Reporter: kmike | Owner: merb
Type: New feature | Status: assigned
Component: Forms | Version:

Severity: Normal | Resolution:
Keywords: form errors | Triage Stage: Accepted
serialization | Needs documentation: 1
Has patch: 1 | Patch needs improvement: 1

Needs tests: 1 | UI/UX: 0
Easy pickings: 1 |
-------------------------------------+-------------------------------------
Changes (by merb):

* needs_better_patch: 0 => 1


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

Django

unread,
Jul 29, 2013, 2:06:39 PM7/29/13
to django-...@googlegroups.com
#17413: Serialization or getting non-HTML version of form errors is not easy
-------------------------------------+-------------------------------------
Reporter: kmike | Owner: merb
Type: New feature | Status: assigned
Component: Forms | Version:

Severity: Normal | Resolution:
Keywords: form errors | Triage Stage: Accepted
serialization | Needs documentation: 1
Has patch: 1 | Patch needs improvement: 1
Needs tests: 1 | UI/UX: 0
Easy pickings: 1 |
-------------------------------------+-------------------------------------

Comment (by merb):

As i asked here: https://github.com/django/django/pull/1406
What's better, to have a errors_dict or a to_json() in the ErrorDict()

the first would make some things like this happen: json.dumps(errors_dict)
the second would be form.errors.to_json()

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

Django

unread,
Aug 10, 2013, 2:14:11 AM8/10/13
to django-...@googlegroups.com
#17413: Serialization or getting non-HTML version of form errors is not easy
-------------------------------------+-------------------------------------
Reporter: kmike | Owner: merb
Type: New feature | Status: assigned
Component: Forms | Version:

Severity: Normal | Resolution:
Keywords: form errors | Triage Stage: Accepted
serialization | Needs documentation: 1
Has patch: 1 | Patch needs improvement: 1
Needs tests: 1 | UI/UX: 0
Easy pickings: 1 |
-------------------------------------+-------------------------------------

Comment (by loic84):

It's probably an unusual request (and hopefully an acceptable one) but I'd
like to put the `ErrorDict.to_json()` effort on-hold.

I'll write more thoroughly about it next week on the ML but here is a
quick explanation of my rationale.

Some background:

- The `ValidationError` refactor
[f34cfec0fa1243b4a3b9865b961a2360f211f0d8] enabled pertaining error
metadata like error code and error params.
- This fixed #20199 and #16986 by allowing metadata to flow freely between
the `Model` layer and the `Form` layer.
- My current work on #20867 will enable raising exceptions with all
metadata from `Form.clean()`.

The issue:

While `ValidationError` is now a capable carrier for errors with their
metadata, all information is lost as soon as it reaches `ErrorDict` and
`ErrorList`. I'd like to fix that by making those two classes capable of
handling instances of `ValidationError`. This is the last piece of the
puzzle, but it'll require a small backward incompatibility that will need
to be discussed (basically changing the output of
`ValidationError.__str__`) to allow `Error(Dict|List)` itself to remain
backward compatible.

Once this has happened, `to_json()` would become much more useful because
on top of having the error message, it will also have the error code,
which will be very useful on the JS side. If we introduce `to_json()` now,
we'll be unable to make this change because of backward compatibility.

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

Django

unread,
Aug 10, 2013, 3:55:12 AM8/10/13
to django-...@googlegroups.com
#17413: Serialization or getting non-HTML version of form errors is not easy
-------------------------------------+-------------------------------------
Reporter: kmike | Owner: merb
Type: New feature | Status: assigned
Component: Forms | Version:

Severity: Normal | Resolution:
Keywords: form errors | Triage Stage: Accepted
serialization | Needs documentation: 1
Has patch: 1 | Patch needs improvement: 1
Needs tests: 1 | UI/UX: 0
Easy pickings: 1 |
-------------------------------------+-------------------------------------

Comment (by merb):

Yeah that's okai for me. I had an working version these days, but my hard
drive crashed. I first need to recover it. (and hope it will work) just
write here when you are done.

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

Django

unread,
Aug 16, 2013, 11:28:40 AM8/16/13
to django-...@googlegroups.com
#17413: Serialization or getting non-HTML version of form errors is not easy
-------------------------------------+-------------------------------------
Reporter: kmike | Owner: merb
Type: New feature | Status: assigned
Component: Forms | Version:

Severity: Normal | Resolution:
Keywords: form errors | Triage Stage: Accepted
serialization | Needs documentation: 1
Has patch: 1 | Patch needs improvement: 1
Needs tests: 1 | UI/UX: 0
Easy pickings: 1 |
-------------------------------------+-------------------------------------

Comment (by loic84):

Some updates:

- #20867 is shaping up nicely and provides the groundwork for this
feature, @timo mentioned he'll review it once 1.6 has been released.
- The experimental branch
https://github.com/loic/django/compare/forms.errordict made `ErrorDict`
aware of metadata, and implements `as_json()` to give a useful
representation of the form errors along with all metadata (see test case).

This will be quite useful to anyone who want to further process the errors
with JavaScript.

Now there are some remaining challenges in term of backward compatibility,
I'll be focussing on that from now on.

--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:10>

Django

unread,
Aug 17, 2013, 4:13:45 AM8/17/13
to django-...@googlegroups.com
#17413: Serialization or getting non-HTML version of form errors is not easy
-------------------------------------+-------------------------------------
Reporter: kmike | Owner: merb
Type: New feature | Status: assigned
Component: Forms | Version:

Severity: Normal | Resolution:
Keywords: form errors | Triage Stage: Accepted
serialization | Needs documentation: 1
Has patch: 1 | Patch needs improvement: 1
Needs tests: 1 | UI/UX: 0
Easy pickings: 1 |
-------------------------------------+-------------------------------------

Comment (by loic84):

Now with backward compatibility:
https://github.com/django/django/pull/1483.

Still missing tons of docs, especially since this builds on top of #20867
and we haven't decided yet on what will be the public API.

I also want to take this opportunity to create an exhaustive test suite
for `ErrorDict` and `ErrorList`, these are tested indirectly by a
gazillion of other tests, but I'd like to have a proper reference test
suite for them.

--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:11>

Django

unread,
Aug 17, 2013, 4:15:22 AM8/17/13
to django-...@googlegroups.com
#17413: Serialization or getting non-HTML version of form errors is not easy
-------------------------------------+-------------------------------------
Reporter: kmike | Owner: loic84

Type: New feature | Status: assigned
Component: Forms | Version: master

Severity: Normal | Resolution:
Keywords: form errors | Triage Stage: Accepted
serialization | Needs documentation: 1
Has patch: 1 | Patch needs improvement: 1
Needs tests: 1 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by loic84):

* owner: merb => loic84
* version: => master
* easy: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:12>

Django

unread,
Nov 29, 2013, 3:33:25 PM11/29/13
to django-...@googlegroups.com
#17413: Serialization or getting non-HTML version of form errors is not easy
-------------------------------------+-------------------------------------
Reporter: kmike | Owner: loic84
Type: New feature | Status: assigned
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: form errors | Triage Stage: Accepted
serialization | Needs documentation: 1
Has patch: 1 | Patch needs improvement: 1
Needs tests: 1 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by loic84):

Following the merge of #20867, I've been able to resume work on this PR:

https://github.com/django/django/pull/1483

Feedback welcome.

--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:13>

Django

unread,
Dec 15, 2013, 12:33:46 PM12/15/13
to django-...@googlegroups.com
#17413: Serialization or getting non-HTML version of form errors is not easy
-------------------------------------+-------------------------------------
Reporter: kmike | Owner: loic84
Type: New feature | Status: assigned
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: form errors | Triage Stage: Ready for
serialization | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* needs_docs: 1 => 0
* needs_better_patch: 1 => 0
* needs_tests: 1 => 0
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:14>

Django

unread,
Dec 16, 2013, 5:40:34 PM12/16/13
to django-...@googlegroups.com
#17413: Serialization or getting non-HTML version of form errors is not easy
-------------------------------------+-------------------------------------
Reporter: kmike | Owner: loic84
Type: New feature | Status: closed
Component: Forms | Version: master
Severity: Normal | Resolution: fixed

Keywords: form errors | Triage Stage: Ready for
serialization | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

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


Comment:

In [changeset:"3ce9829b615336b0f3ac39b080c27fc8cf5af483"]:
{{{
#!CommitTicketReference repository=""
revision="3ce9829b615336b0f3ac39b080c27fc8cf5af483"
Fixed #17413 -- Serialization of form errors along with all metadata.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:15>

Django

unread,
Dec 18, 2013, 9:00:25 AM12/18/13
to django-...@googlegroups.com
#17413: Serialization or getting non-HTML version of form errors is not easy
-------------------------------------+-------------------------------------
Reporter: kmike | Owner: loic84
Type: New feature | Status: closed
Component: Forms | Version: master
Severity: Normal | Resolution: fixed
Keywords: form errors | Triage Stage: Ready for
serialization | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

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

In [changeset:"2fd7fc134cf0c0685ceac22fd858509aa43f819f"]:
{{{
#!CommitTicketReference repository=""
revision="2fd7fc134cf0c0685ceac22fd858509aa43f819f"
Refs #17413 -- Added isinstance backward compatibility on ErrorList.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:16>

Django

unread,
Mar 6, 2014, 12:35:04 PM3/6/14
to django-...@googlegroups.com
#17413: Serialization or getting non-HTML version of form errors is not easy
-------------------------------------+-------------------------------------
Reporter: kmike | Owner: loic84
Type: New feature | Status: closed
Component: Forms | Version: master
Severity: Normal | Resolution: fixed
Keywords: form errors | Triage Stage: Ready for
serialization | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

Comment (by Florian Apolloner <florian@…>):

In [changeset:"34236efc5e569ce7d42f7d52dd798e59f95457f8"]:
{{{
#!CommitTicketReference repository=""
revision="34236efc5e569ce7d42f7d52dd798e59f95457f8"
Reworked ErrorDict.as_json() to prevent unnecessary
serialization/deserialization step.

Thanks @apollo13 for the suggestion. Refs #17413.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:17>

Reply all
Reply to author
Forward
0 new messages