Opened 4 years ago

Closed 3 years ago

Last modified 3 years ago

#32807 closed Bug (fixed)

JSONField.bound_data raises TypeError when data is None

Reported by: Dan Strokirk Owned by: Dan Strokirk
Component: Forms Version: 3.2
Severity: Normal Keywords: JSONField
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Hi,

When a bound form with no value provided for the JSONField is rendered, it will currently crash.

class JSONForm(Form):
    json_field = JSONField(required=False)
form = JSONForm({})
assert form.as_p()

raises

TypeError: the JSON object must be str, bytes or bytearray, not NoneType

A fix has been created already by @AlexHill here: https://github.com/django/django/pull/13844

Change History (9)

comment:1 by Mariusz Felisiak, 4 years ago

Owner: changed from nobody to Alex Hill
Status: newassigned
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:2 by Mariusz Felisiak, 4 years ago

comment:3 by Mariusz Felisiak, 4 years ago

Needs tests: set
Patch needs improvement: set

comment:4 by Jacob Walls, 4 years ago

comment:6 by Mariusz Felisiak, 3 years ago

Needs tests: unset
Owner: changed from Alex Hill to Dan Strokirk
Patch needs improvement: unset

comment:7 by Mariusz Felisiak, 3 years ago

Triage Stage: AcceptedReady for checkin

comment:8 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In f5ea9aa2:

Fixed #32807 -- Fixed JSONField crash when redisplaying None values.

Thanks to Alex Hill for the initial patch.

comment:9 by Timothy Hobbs, 3 years ago

Hi, was this fixed already in the release Django 3.2.7? I'm getting this backtrace on Django 3.2.7 when running admin smoke tests and the backtrace doesn't touch my code at all, making it a bit tricky to debug:

======================================================================
ERROR: test_change_post (apps.aklub.tests.test_admin.AdminSmokeTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/klub-v/venv/src/django-admin-smoke-tests-2/django_admin_smoke_tests/tests.py", line 31, in test_deco
    fn(self, model, model_admin)
  File "/klub-v/venv/src/django-admin-smoke-tests-2/django_admin_smoke_tests/tests.py", line 307, in test_change_post
    response.render()
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/response.py", line 105, in render
    self.content = self.rendered_content
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/response.py", line 83, in rendered_content
    return template.render(context, self._request)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 170, in render
    return self._render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/test/utils.py", line 100, in instrumented_test_render
    return self.nodelist.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/test/utils.py", line 100, in instrumented_test_render
    return self.nodelist.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/test/utils.py", line 100, in instrumented_test_render
    return self.nodelist.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/test/utils.py", line 100, in instrumented_test_render
    return self.nodelist.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/test/utils.py", line 100, in instrumented_test_render
    return self.nodelist.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/test/utils.py", line 100, in instrumented_test_render
    return self.nodelist.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/test/utils.py", line 100, in instrumented_test_render
    return self.nodelist.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/defaulttags.py", line 211, in render
    nodelist.append(node.render_annotated(context))
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/loader_tags.py", line 195, in render
    return template.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 172, in render
    return self._render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/test/utils.py", line 100, in instrumented_test_render
    return self.nodelist.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/defaulttags.py", line 211, in render
    nodelist.append(node.render_annotated(context))
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/defaulttags.py", line 211, in render
    nodelist.append(node.render_annotated(context))
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/defaulttags.py", line 312, in render
    return nodelist.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/defaulttags.py", line 312, in render
    return nodelist.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 994, in render
    return render_value_in_context(output, context)
  File "/klub-v/venv/lib/python3.8/site-packages/django/template/base.py", line 973, in render_value_in_context
    value = str(value)
  File "/klub-v/venv/lib/python3.8/site-packages/django/utils/html.py", line 376, in <lambda>
    klass.__str__ = lambda self: mark_safe(klass_str(self))
  File "/klub-v/venv/lib/python3.8/site-packages/django/forms/boundfield.py", line 34, in __str__
    return self.as_widget()
  File "/klub-v/venv/lib/python3.8/site-packages/django/forms/boundfield.py", line 95, in as_widget
    value=self.value(),
  File "/klub-v/venv/lib/python3.8/site-packages/django/forms/boundfield.py", line 130, in value
    data = self.field.bound_data(self.data, data)
  File "/klub-v/venv/lib/python3.8/site-packages/django/forms/fields.py", line 1265, in bound_data
    return json.loads(data, cls=self.decoder)
  File "/usr/local/lib/python3.8/json/__init__.py", line 341, in loads
    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not NoneType
Note: See TracTickets for help on using tickets.
Back to Top