Object of type 'UUID' is not JSON serializable
See original GitHub issueI have stack daphe+asgi+djangochannelsrestframework for working with websockets. djangochannelsrestframework uses django channels under the hood, as I understand. So, I have 3 simple models:
import uuid
from django.db import models
class App(models.Model):
    key = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    short_name = models.CharField(max_length=100)
class Token(models.Model):
    token = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    app = models.ForeignKey(App, on_delete=models.CASCADE)
class Phone(models.Model):
    token = models.OneToOneField(Token, primary_key=True, on_delete=models.CASCADE)
    phone = models.CharField(max_length=12)
My simple serializers:
from .models import App, Token, Phone
from rest_framework import serializers
class AppSerializer(serializers.ModelSerializer):
    class Meta:
        model = App
        fields = ['key', 'short_name']
class TokenSerializer(serializers.ModelSerializer):
    class Meta:
        model = Token
        fields = ['token', 'app']
class PhoneSerializer(serializers.ModelSerializer):
    class Meta:
        model = Phone
        fields = ['phone', 'token']
So, when I try to retrieve a phone via ws I get an error:
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/channels/routing.py", line 71, in __call__
    return await application(scope, receive, send)
  File "/usr/local/lib/python3.6/site-packages/channels/routing.py", line 160, in __call__
    send,
  File "/usr/local/lib/python3.6/site-packages/channels/consumer.py", line 94, in app
    return await consumer(scope, receive, send)
  File "/usr/local/lib/python3.6/site-packages/channelsmultiplexer/demultiplexer.py", line 61, in __call__
    await future
  File "/usr/local/lib/python3.6/site-packages/channels/consumer.py", line 94, in app
    return await consumer(scope, receive, send)
  File "/usr/local/lib/python3.6/site-packages/channels/consumer.py", line 62, in __call__
    await await_many_dispatch([receive], self.dispatch)
  File "/usr/local/lib/python3.6/site-packages/channels/utils.py", line 51, in await_many_dispatch
    await dispatch(result)
  File "/usr/local/lib/python3.6/site-packages/channels/consumer.py", line 73, in dispatch
    await handler(message)
  File "/usr/local/lib/python3.6/site-packages/channels/generic/websocket.py", line 196, in websocket_receive
    await self.receive(text_data=message["text"])
  File "/usr/local/lib/python3.6/site-packages/channels/generic/websocket.py", line 259, in receive
    await self.receive_json(await self.decode_json(text_data), **kwargs)
  File "/usr/local/lib/python3.6/site-packages/djangochannelsrestframework/consumers.py", line 160, in receive_json
    await self.handle_action(action, request_id=request_id, **content)
  File "/usr/local/lib/python3.6/site-packages/djangochannelsrestframework/consumers.py", line 151, in handle_action
    await self.handle_exception(exc, action=action, request_id=request_id)
  File "/usr/local/lib/python3.6/site-packages/djangochannelsrestframework/consumers.py", line 117, in handle_exception
    raise exc
  File "/usr/local/lib/python3.6/site-packages/djangochannelsrestframework/consumers.py", line 148, in handle_action
    await reply(data=data, status=status)
  File "/usr/local/lib/python3.6/site-packages/djangochannelsrestframework/consumers.py", line 177, in reply
    await self.send_json(payload)
  File "/usr/local/lib/python3.6/site-packages/channels/generic/websocket.py", line 273, in send_json
    await super().send(text_data=await self.encode_json(content), close=close)
  File "/usr/local/lib/python3.6/site-packages/channels/generic/websocket.py", line 281, in encode_json
    return json.dumps(content)
  File "/usr/local/lib/python3.6/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/local/lib/python3.6/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/local/lib/python3.6/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/local/lib/python3.6/json/encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'UUID' is not JSON serializable
Please, help me to find out the reason it!
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (2 by maintainers)
 Top Results From Across the Web
Top Results From Across the Web
UUID('...') is not JSON serializable - python - Stack Overflow
Looks like this error is no more relevant for django, while using DjangoJSONEncoder . No need to write own ...
Read more >Object of type UUID is not JSON serializable #586 - GitHub
I'm passing in a UUID as a part of my extra data and getting this error Object of type UUID is not JSON...
Read more >Fixing UUID is not JSON serializable - Arthur Pemberton
While using django-eztables to do the server-side work for DataTables.net, I ran into an issue with a UUID field. Exception Type: TypeError
Read more >Mailman 3 JSON Serializing UUID objects - Python-ideas
The issue with custom types serialized/unserialized with JSON is that they don't exist in JSON format, so you need to find a way...
Read more >tiangolo/fastapi - Gitter
dict() returning UUID type which can not be serialized... so the solution could be json .dumps -> json.loads - but this seems to...
Read more > Top Related Medium Post
Top Related Medium Post
No results found
 Top Related StackOverflow Question
Top Related StackOverflow Question
No results found
 Troubleshoot Live Code
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
Top Related Reddit Thread
No results found
 Top Related Hackernoon Post
Top Related Hackernoon Post
No results found
 Top Related Tweet
Top Related Tweet
No results found
 Top Related Dev.to Post
Top Related Dev.to Post
No results found
 Top Related Hashnode Post
Top Related Hashnode Post
No results found

Solved, thank you🙏🙏 @carltongibson
“consumer” did you mean default consumer at my channel module?