Serializing Error in serializer.py
See original GitHub issueI get the following error if I pass this {"id":"NMddZ6vg:requirement-view:1","data":{"is_editing":false,"requirement":{"created":"2021-01-26T10:59:25.399Z","last_updated":"2021-01-26T10:59:28.908Z","name":"Test","description":"Description for Test","pk":1}},"checksum":"2JbJYULj","actionQueue":[{"type":"callMethod","payload":{"name":"edit"},"partial":{}}]}
POST request to the component.
Internal Server Error: /unicorn/message/requirement-view
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/app/.local/lib/python3.8/site-packages/django_unicorn/decorators.py", line 59, in timed
result = wrapped(*args, **kwargs)
File "/app/.local/lib/python3.8/site-packages/django_unicorn/views.py", line 29, in wrapped_view
return view_func(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/django/views/decorators/http.py", line 40, in inner
return func(request, *args, **kwargs)
File "/app/.local/lib/python3.8/site-packages/django_unicorn/views.py", line 357, in message
component_request.data = orjson.loads(component.get_frontend_context_variables())
File "/app/.local/lib/python3.8/site-packages/django_unicorn/decorators.py", line 59, in timed
result = wrapped(*args, **kwargs)
File "/app/.local/lib/python3.8/site-packages/django_unicorn/components.py", line 409, in get_frontend_context_variables
encoded_frontend_context_variables = serializer.dumps(
File "/app/.local/lib/python3.8/site-packages/django_unicorn/serializer.py", line 127, in dumps
serialized_data = orjson.dumps(data, default=_json_serializer)
TypeError: Type is not JSON serializable: Requirement
After some testing and debugging I recognized that the DateTime fields (last_updated
and created
) are the possible pain points.
I made some “debug print()” in the _dumps function
print(data["requirement"])
serialized_data = orjson.dumps(data, default=_json_serializer)
print(serialized_data)
if fix_floats:
return _dumps(serialized_data)
return serialized_data
and got the following output:
1st print:
{'is_editing': False, 'requirement': {'created': '2021-01-26T10:59:25.399Z', 'last_updated': '2021-01-26T10:59:28.908Z', 'name': 'Test', 'description': 'Description for Test', 'pk': 1}}
2nd print:
b'{"is_editing":false,"requirement":{"created":"2021-01-26T10:59:25.399Z","last_updated":"2021-01-26T10:59:28.908Z","name":"Test","description":"Description for Test","pk":1}}'
3rd was the recursive call from fix_floats
:
{'is_editing': True, 'requirement': <Requirement: Test>}
After the 3rd comes the crash - in that case the “requirement” was serialized as Requirement object from my Requirement model.
Can you maybe reproduce the serializing error with this POST data?
If I remove the DateTime fields it works.
In my model I use the created = models.DateTimeField(auto_now_add=True, editable=False)
I can also change the entry in the admin view.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
Yes it works now with the 0.17.2 😉 … Thank you for the fast response!
fix_floats
is only supposed convertfloat
types to strings for JSON, but sounds like there might be a bug.Just to make sure I understand
Requirement
is a Django model (i.e. it inherits fromdjango.db.models.Model
)?I’ll try to reproduce on my own, but if you could write a breaking unit test in this project, fork this project and change a component in the example to break in the same way, or copy/paste your
UnicornView
that is breaking into this issue that would be helpful. Thanks!