Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.

TypeError: Object of type '__proxy__' is not JSON serializable #86

Closed
luanfonceca opened this issue Nov 13, 2017 · 5 comments
Closed

TypeError: Object of type '__proxy__' is not JSON serializable #86

luanfonceca opened this issue Nov 13, 2017 · 5 comments

Comments

@luanfonceca
Copy link

luanfonceca commented Nov 13, 2017

  • drf-openapi==1.0.0
  • Python 3.6.1
  • Django==1.11.5
  • MacOSX

Description

Using ugettext_lazy inside Models break the Open API.

Traceback

Internal Server Error: /api/v1.0/docs/
Traceback (most recent call last):
  File "/Users/.../lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/Users/.../lib/python3.6/site-packages/django/core/handlers/base.py", line 217, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/.../lib/python3.6/site-packages/django/core/handlers/base.py", line 215, in _get_response
    response = response.render()
  File "/Users/.../lib/python3.6/site-packages/django/template/response.py", line 107, in render
    self.content = self.rendered_content
  File "/Users/.../lib/python3.6/site-packages/rest_framework/response.py", line 72, in rendered_content
    ret = renderer.render(self.data, accepted_media_type, context)
  File "/Users/.../lib/python3.6/site-packages/drf_openapi/codec.py", line 113, in render
    return OpenAPICodec().encode(data, extra=extra)
  File "/Users/.../lib/python3.6/site-packages/drf_openapi/codec.py", line 103, in encode
    return force_bytes(json.dumps(data))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type '__proxy__' is not JSON serializable

What I Did

Inside all my Models, i have set i18n strings using ugettext_lazy functional to translate it. If i use the ugettext instead of the lazy version, everything works fine.

from django.utils.translation import ugettext_lazy as _
from django.db import models

class MyModel(models.Model):
    title = models.CharField(
        verbose_name=_('Title'),
        max_length=200, blank=False, null=False,
        help_text=_('A string describing the MyModel title.'))

cc @fernandolins

@werwty
Copy link
Collaborator

werwty commented Nov 13, 2017

@luanfonceca I don't think this is a drf_openapi issue. It looks like you're trying to serialize a lazy translation object instead of a string.
Django recommends passing the translation object to str() to generate a string in the current locale: https://docs.djangoproject.com/en/dev/ref/unicode/#translated-strings

@luanfonceca
Copy link
Author

Thats also happen if we use the User models.

@luanfonceca
Copy link
Author

luanfonceca commented Nov 13, 2017

Hi @werwty for me, that process of evaluating a string should happen inside the drf_openapi. Its the python json module that doesn't handle lazy objects very well, but inside this module, we need to evaluate it, the entire django models is designed to work with lazy translations in their verbose_name, help_text and etc.

Can you help me out on what can i do in order to make that work?
For instance, if i want to create a user serializer, using the default django user's models, what should i do to the drf_openapi show the docs right?!

@werwty
Copy link
Collaborator

werwty commented Nov 14, 2017

@luanfonceca I did a quick test of drf_openapi serialization with ugettext_lazy and it looks like it's not breaking drf_openapi.
https://github.com/werwty/rest-framework-tutorial/commits/ugettext_lazy-test
I'd be happy to take another look if you submit a PR against my fork with breaking changes.

@luanfonceca
Copy link
Author

Hi @werwty, when i upgraded to the tip version of drf, it came back to work.

Sorry for that.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants