Skip to content

TypeError: Object of type Hashid is not JSON serializable #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
icemac opened this issue Sep 23, 2022 · 3 comments
Closed

TypeError: Object of type Hashid is not JSON serializable #33

icemac opened this issue Sep 23, 2022 · 3 comments
Assignees

Comments

@icemac
Copy link

icemac commented Sep 23, 2022

I saw the following error:

Exception in thread kolo-save_request_in_db:
Traceback (most recent call last):
  File ".../lib/python3.9/threading.py", line 980, in _bootstrap_inner
[23/Sep/2022 15:45:28] "POST /api/v1/treatment_request/ HTTP/1.0" 201 116
    self.run()
  File ".../lib/python3.9/threading.py", line 917, in run
    self._target(*self._args, **self._kwargs)
  File ".../kolo-2.0.3-py3.9.egg/kolo/profiler.py", line 233, in save_request_in_db
    self.db_path, self.trace_id, json.dumps(json_data), wal_mode
  File ".../lib/python3.9/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File ".../lib/python3.9/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File ".../lib/python3.9/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File ".../lib/python3.9/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Hashid is not JSON serializable

I believe the Hashid comes from django-hashid-field.

Python: 3.9
Django: 4.0.7
kolo: 2.0.3
django-hashid-field: 3.3.5

@LilyAcorn
Copy link
Contributor

@icemac I've not managed to replicate this failure. Can you share more information about the code you were running which caused this?

@icemac
Copy link
Author

icemac commented Oct 10, 2022

I am using djangorestframework and https://github.com/nshafer/django-hashid-field

from django.contrib.auth.models import AbstractUser
from hashid_field import HashidAutoField

class MyUser(AbstractUser):

    id = HashidAutoField(
        verbose_name=_('id'),
        primary_key=True,
        editable=False)

I think, I accessed a view which serializes the model to JSON.

@LilyAcorn
Copy link
Contributor

This will be fixed in the next release.

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

No branches or pull requests

2 participants