#33130 closed Bug (fixed)
ErrorDict doesn't pass the dict type check in JsonResponse.
Reported by: | Jaap Roes | Owned by: | Jaap Roes |
---|---|---|---|
Component: | Forms | Version: | 4.0 |
Severity: | Release blocker | Keywords: | |
Cc: | David Smith | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Just ran the test suite of a project on Django 4.0a1 (with Python 3.8) and ran into the following issue:
We have a fairly simple FormView
that defines the following form_invalid
method:
def form_invalid(self, form): return JsonResponse(form.errors, status=400)
This worked fine before, but with Django 4 this raises the following error:
TypeError: In order to allow non-dict objects to be serialized set the safe parameter to False.
Wrapping form.errors
with dict()
fixes this, but I'm not sure this should be necessary.
Change History (10)
comment:1 by , 3 years ago
Cc: | added |
---|---|
Severity: | Normal → Release blocker |
Summary: | ErrorDict doesn't pass the dict type check in JsonResponse → ErrorDict doesn't pass the dict type check in JsonResponse. |
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 3 years ago
It should be enough to subclass dict
:
class ErrorDict(UserDict, dict, RenderableErrorMixin):
comment:3 by , 3 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:4 by , 3 years ago
Has patch: | set |
---|
PR (There are two mariadb failures, but those seem unrelated)
comment:5 by , 3 years ago
Needs tests: | set |
---|---|
Patch needs improvement: | set |
comment:6 by , 3 years ago
Sadly the fix isn't as simple as initially thought (comment:2). It solves the issue with the type check in JsonResponse
but the actual json serialization results in an empty JSON object instead of the intended result of a JSON object with field names and error messages.
I will write additional tests to also check if the ErrorDict
also serializes in the desired way, and come up with a better fix (the PR comments already have a good indication to what that may be).
comment:7 by , 3 years ago
Needs tests: | unset |
---|---|
Patch needs improvement: | unset |
I've updated the PR with a JSON serialization tests and a modified fix so make the JSON serialization test pass.
comment:8 by , 3 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Thanks for the report!
Form.errors
should be recognizable as adict
according to docs.Regression in 456466d932830b096d39806e291fe23ec5ed38d5.