django-channel error

dooh kim·2020년 4월 20일
0

계속해서 JSON type 에 대한 에러 때문에
serialize 문제가 생기는 것 같다
그래서

json.dumps 를 쓰는 방법을 썼지만 해결되지 않았다.

문제는

Exception inside application: can not serialize 'UserLazyObject' object
Traceback (most recent call last):
File "/home/kimdooh/.pyenv/versions/django-channel-lecture-env/lib/python3.7/site-packages/channels/sessions.py", line 183, in call
return await self.inner(receive, self.send)
File "/home/kimdooh/.pyenv/versions/django-channel-lecture-env/lib/python3.7/site-packages/channels/middleware.py", line 41, in coroutine_call
await inner_instance(receive, send)
File "/home/kimdooh/.pyenv/versions/django-channel-lecture-env/lib/python3.7/site-packages/channels/consumer.py", line 59, in call
[receive, self.channel_receive], self.dispatch
File "/home/kimdooh/.pyenv/versions/django-channel-lecture-env/lib/python3.7/site-packages/channels/utils.py", line 51, in await_many_dispatch
await dispatch(result)
File "/home/kimdooh/.pyenv/versions/django-channel-lecture-env/lib/python3.7/site-packages/channels/consumer.py", line 73, in dispatch
await handler(message)
File "/home/kimdooh/.pyenv/versions/django-channel-lecture-env/lib/python3.7/site-packages/channels/generic/websocket.py", line 196, in websocket_receive
await self.receive(text_data=message["text"])
File "/home/kimdooh/projects/fastcampus/project_with_team/personal-projects/django-channel-lecture/app/chat/consumers.py", line 50, in receive
'room_name' : room_name
File "/home/kimdooh/.pyenv/versions/django-channel-lecture-env/lib/python3.7/site-packages/channels_redis/core.py", line 628, in group_send
) = self._map_channel_keys_to_connection(channel_names, message)
File "/home/kimdooh/.pyenv/versions/django-channel-lecture-env/lib/python3.7/site-packages/channels_redis/core.py", line 751, in _map_channel_keys_to_connection
channel_key_to_message[key] = self.serialize(channel_key_to_message[key])
File "/home/kimdooh/.pyenv/versions/django-channel-lecture-env/lib/python3.7/site-packages/channels_redis/core.py", line 771, in serialize
value = msgpack.packb(message, use_bin_type=True)
File "/home/kimdooh/.pyenv/versions/django-channel-lecture-env/lib/python3.7/site-packages/msgpack/init.py", line 46, in packb
return Packer(**kwargs).pack(o)
File "msgpack/_packer.pyx", line 284, in msgpack._cmsgpack.Packer.pack
File "msgpack/_packer.pyx", line 290, in msgpack._cmsgpack.Packer.pack
File "msgpack/_packer.pyx", line 287, in msgpack._cmsgpack.Packer.pack
File "msgpack/_packer.pyx", line 234, in msgpack._cmsgpack.Packer._pack
File "msgpack/_packer.pyx", line 281, in msgpack._cmsgpack.Packer._pack
TypeError: can not serialize 'UserLazyObject' object

해결책!!

    async def receive(self, text_data):
        text_data_json = json.loads(text_data)
        print(text_data_json)
        message = text_data_json['message']
        room_name = text_data_json['roomName']
        try:
            user = self.scope["user"]
        except KeyError:
            raise ClientError("USER MISS")
            
        chat_model = await create_log_or_error(room_name, user, message)
        # Send message to room group
        await self.channel_layer.group_send(
            self.room_group_name,
            {
                'type': 'chat_message',
                'message' : message,
                'room_name' : room_name,
                'user': self.scope['user']
            }
        )

self.scope['user']
이 부분에서 문제가 생겼다
다른 예제를 보니
self.scope['user'].username
이렇게 사용하는 걸 보니 이부분에서 python type으로 보내진것으로
추측된다.

profile
testify to the light

0개의 댓글