question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serializing Error in serializer.py

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sbidycommented, Jan 28, 2021

Yes it works now with the 0.17.2 😉 … Thank you for the fast response!

1reaction
adamghillcommented, Jan 26, 2021

fix_floats is only supposed convert float 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 from django.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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Serializers - Django REST framework
Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily ...
Read more >
Writing serializer errors to the logger. Django rest
I successfully record all completed requests, but I cannot figure out how to write validation errors of the serializer, for example, ...
Read more >
serialize-error - npm
Serialize /deserialize an error into a plain object. Useful if you for example need to JSON.stringify() or process.send() the error.
Read more >
Serializers - Django REST framework - Tom Christie
Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered...
Read more >
Serializers | Sentry Developer Documentation
Serializers are used to take complex python models and translate them into json. Serializers can also be used to deserialize json back to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found