Object of type 'Hashid' is not JSON serializable
See original GitHub issueThe regular JSON encoders can’t handle the Hashid object, so we had to override the encoder to use the hashed value instead of the instance:
class HashidJSONEncoder(DjangoJSONEncoder):
def default(self, o):
if isinstance(o, Hashid):
return str(o)
return super().default(o)
This is not that surprising or new, but I’d leave a note.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:7 (1 by maintainers)
Top Results From Across the Web
TypeError: Object of type date is not JSON serializable
The query_dict contains date objects which are not JSON serializable and by default Django will serialize session data using JSON.
Read more >django-hashid-field - PyPI
Allows lookups and filtering by hashid string or Hashid object and ... don't you will get an error such as “Hashid(60): N8VNa8z is...
Read more >Django object is not JSON serializable | Edureka Community
I have the following code for serializing the queryset; def render_to_response(self, context, ** ... both django objects and dicts.
Read more >[Solved] DJANGO: object of type map is not JSON serializable
The map() function[^] returns an iterator and you must convert it to a list before you can serialize it. Python.
Read more >HashIds Support - djangorestframework-serializer-extensions
Enabling HashIds. Serializer extensions also provides support for HashIds. Publicly exposing database IDs to your end users is often not a good idea, ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Make sure that each models.ForeignKey is declared in Serializer:
like this:
chinese version: 在model里,所有用了models.ForeignKey的字段, 在Serializer文件里,必须 用这种格式:
不然就会提示错误, 而且很难排查 , 我花了一整天都没有找出原因,最后通过删减字段,才搞定
I have the issue when the package django-impersonate push the hashid of my customer user class in the session and django tries to sign the session. For this, django convert the content of the session to JSON which triggers the exception.
The content of the session
data
(insession_data=self.encode(data),
in the traceback) that will be JSON serialized is{..., '_impersonate': Hashid(2): b3ZRvZr, '_impersonate_start': 1607524600.914942, ...}
.