django-admin-interface: Object of type Theme is not JSON serializable

Python version 3.10.11

Django version 4.0.10

Package version 0.28.3

Current behavior (bug description) On startup the project encounters an error

Desired behavior Project should start without error

Hi there! I’m really interested in using this package for my team’s project, but on startup I’m receiving the following stacktrace. It looks like it may not be playing nicely with some of the dependency packages our project has, as this package doesn’t encounter this error when I install it on a fresh project.

Has this issue been encountered by anyone before? Is there a known remedy? Thanks in advance!

Traceback (most recent call last):
  File ".../.venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 56, in inner
    response = get_response(request)
  File ".../.venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 220, in _get_response
    response = response.render()
  File ".../.venv/lib/python3.10/site-packages/sentry_sdk/integrations/django/views.py", line 38, in sentry_patched_render
    return old_render(self)
  File ".../.venv/lib/python3.10/site-packages/django/template/response.py", line 114, in render
    self.content = self.rendered_content
  File ".../.venv/lib/python3.10/site-packages/sentry_sdk/integrations/django/templates.py", line 74, in rendered_content
    return real_rendered_content.fget(self)
  File ".../.venv/lib/python3.10/site-packages/django/template/response.py", line 92, in rendered_content
    return template.render(context, self._request)
  File ".../.venv/lib/python3.10/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File ".../.venv/lib/python3.10/site-packages/django/template/base.py", line 175, in render
    return self._render(context)
  File ".../.venv/lib/python3.10/site-packages/django/test/utils.py", line 111, in instrumented_test_render
    return self.nodelist.render(context)
  File ".../.venv/lib/python3.10/site-packages/django/template/base.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File ".../.venv/lib/python3.10/site-packages/django/template/base.py", line 1000, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File ".../.venv/lib/python3.10/site-packages/django/template/base.py", line 958, in render_annotated
    return self.render(context)
  File ".../.venv/lib/python3.10/site-packages/django/template/loader_tags.py", line 157, in render
    return compiled_parent._render(context)
  File ".../.venv/lib/python3.10/site-packages/django/test/utils.py", line 111, in instrumented_test_render
    return self.nodelist.render(context)
  File ".../.venv/lib/python3.10/site-packages/django/template/base.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File ".../.venv/lib/python3.10/site-packages/django/template/base.py", line 1000, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File ".../.venv/lib/python3.10/site-packages/django/template/base.py", line 958, in render_annotated
    return self.render(context)
  File ".../.venv/lib/python3.10/site-packages/django/template/loader_tags.py", line 157, in render
    return compiled_parent._render(context)
  File ".../.venv/lib/python3.10/site-packages/django/test/utils.py", line 111, in instrumented_test_render
    return self.nodelist.render(context)
  File ".../.venv/lib/python3.10/site-packages/django/template/base.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File ".../.venv/lib/python3.10/site-packages/django/template/base.py", line 1000, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File ".../.venv/lib/python3.10/site-packages/django/template/base.py", line 958, in render_annotated
    return self.render(context)
  File ".../.venv/lib/python3.10/site-packages/django/template/loader_tags.py", line 157, in render
    return compiled_parent._render(context)
  File ".../.venv/lib/python3.10/site-packages/django/test/utils.py", line 111, in instrumented_test_render
    return self.nodelist.render(context)
  File ".../.venv/lib/python3.10/site-packages/django/template/base.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File ".../.venv/lib/python3.10/site-packages/django/template/base.py", line 1000, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File ".../.venv/lib/python3.10/site-packages/django/template/base.py", line 958, in render_annotated
    return self.render(context)
  File ".../.venv/lib/python3.10/site-packages/django/template/loader_tags.py", line 157, in render
    return compiled_parent._render(context)
  File ".../.venv/lib/python3.10/site-packages/django/test/utils.py", line 111, in instrumented_test_render
    return self.nodelist.render(context)
  File ".../.venv/lib/python3.10/site-packages/django/template/base.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File ".../.venv/lib/python3.10/site-packages/django/template/base.py", line 1000, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File ".../.venv/lib/python3.10/site-packages/django/template/base.py", line 958, in render_annotated
    return self.render(context)
  File ".../.venv/lib/python3.10/site-packages/django/template/loader_tags.py", line 63, in render
    result = block.nodelist.render(context)
  File ".../.venv/lib/python3.10/site-packages/django/template/base.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File ".../.venv/lib/python3.10/site-packages/django/template/base.py", line 1000, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File ".../.venv/lib/python3.10/site-packages/django/template/base.py", line 958, in render_annotated
    return self.render(context)
  File ".../.venv/lib/python3.10/site-packages/django/template/library.py", line 239, in render
    output = self.func(*resolved_args, **resolved_kwargs)
  File ".../.venv/lib/python3.10/site-packages/admin_interface/templatetags/admin_interface_tags.py", line 64, in get_admin_interface_theme
    set_cached_active_theme(theme)
  File ".../.venv/lib/python3.10/site-packages/admin_interface/cache.py", line 18, in set_cached_active_theme
    app_cache().set("admin_interface_theme", theme)
  File ".../.venv/lib/python3.10/site-packages/debug_toolbar/panels/cache.py", line 40, in wrapped
    value = method(self, *args, **kwargs)
  File ".../.venv/lib/python3.10/site-packages/debug_toolbar/panels/cache.py", line 95, in set
    return self.cache.set(*args, **kwargs)
  File ".../.venv/lib/python3.10/site-packages/django_redis/cache.py", line 31, in _decorator
    return method(self, *args, **kwargs)
  File ".../.venv/lib/python3.10/site-packages/django_redis/cache.py", line 80, in set
    return self.client.set(*args, **kwargs)
  File ".../.venv/lib/python3.10/site-packages/django_redis/client/default.py", line 143, in set
    nvalue = self.encode(value)
  File ".../.venv/lib/python3.10/site-packages/django_redis/client/default.py", line 457, in encode
    value = self._serializer.dumps(value)
  File ".../.venv/lib/python3.10/site-packages/django_redis/serializers/json.py", line 13, in dumps
    return json.dumps(value, cls=self.encoder_class).encode()
  File ".../.asdf/installs/python/3.10.11/lib/python3.10/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File ".../.asdf/installs/python/3.10.11/lib/python3.10/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File ".../.asdf/installs/python/3.10.11/lib/python3.10/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File ".../.venv/lib/python3.10/site-packages/django/core/serializers/json.py", line 106, in default
    return super().default(o)
  File ".../.asdf/installs/python/3.10.11/lib/python3.10/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '

Exception Type: TypeError at /admin/login/
Exception Value: Object of type Theme is not JSON serializable

Upvote & Fund

  • We’re using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
<picture> <source media="(prefers-color-scheme: dark)" srcset="https://polar.sh/api/github/fabiocaccamo/django-admin-interface/issues/356/pledge.svg?darkmode=1"> Fund with Polar </picture>

About this issue

  • Original URL
  • State: closed
  • Created 5 months ago
  • Comments: 20 (20 by maintainers)

Most upvoted comments

@TheBitShepherd that would be great, thanks!

@merwok - Yes. As I mentioned above configuring a different cache for this package was one way I found to alleviate the error. 👍

The most minimal repro

That’s only showing the surface cause! We already knew from the original traceback that this particular call is a problem. The thing I wanted to see is: why does the combination of django-redis and django-admin-interface causes the call to happen 🙂 → a repro of minimal django project (dependencies and settings)

@merwok - The most minimal repro I’ve been able to come up with is the following in a django shell after installing this package:

$ theme = Theme.objects.get()
$ theme
 <Theme: Django>
$ from json import JSONEncoder
$ chunks = JSONEncoder().iterencode(theme, _one_shot=True)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
----> 1 chunks = JSONEncoder().iterencode(theme, _one_shot=True)

~/.asdf/installs/python/3.10.11/lib/python3.10/json/encoder.py in iterencode(self, o, _one_shot)
    255                 self.key_separator, self.item_separator, self.sort_keys,
    256                 self.skipkeys, _one_shot)
--> 257         return _iterencode(o, 0)
    258
    259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,

~/.asdf/installs/python/3.10.11/lib/python3.10/json/encoder.py in default(self, o)
    177
    178         """
--> 179         raise TypeError(f'Object of type {o.__class__.__name__} '
    180                         f'is not JSON serializable')
    181

TypeError: Object of type Theme is not JSON serializable

I can work on making a smaller project to replicate but that may take some time.