Django channel : Save messages to database

标签 django django-models chat django-channels channels

我是 channel 的新手,我按照他们的官方文档制作了一个聊天室应用程序。现在我正在尝试保存聊天消息。我所知道的是我可以创建一个模型,但我不知道如何将它从consumers.py 保存到我的数据库中。我在消息中添加了用户名。一点帮助将不胜感激。
我的消费者.py:

import json
from channels.generic.websocket import AsyncWebsocketConsumer

class ChatConsumer(AsyncWebsocketConsumer):
    async def connect(self):
        self.room_name = self.scope['url_route']['kwargs']['room_name']
        self.room_group_name = 'chat_%s' % self.room_name

        # Join room group
        await self.channel_layer.group_add(
            self.room_group_name,
            self.channel_name
        )

        await self.accept()

    async def disconnect(self, close_code):
        # Leave room group
        await self.channel_layer.group_discard(
            self.room_group_name,
            self.channel_name
        )

    # Receive message from WebSocket
    async def receive(self, text_data):
        username = self.scope["user"].first_name
        name = self.scope['user'].username
        text_data_json = json.loads(text_data)
        message = text_data_json['message']
        message = (username + '(' + name + ')' + ':\n' + message)

        # Send message to room group
        await self.channel_layer.group_send(
            self.room_group_name,
            {   
                'type': 'chat_message',
                'message': message
            }
        )

    # Receive message from room group
    async def chat_message(self, event):
        username = self.scope["user"].username
        message = event['message']
        name = self.scope["user"].username

        # Send message to WebSocket
        await self.send(text_data=json.dumps({
            'message': message,
            "username": username,
            "name": name
        }))
我保存消息的模型:
class Message(models.Model):
    author = models.ForeignKey(User, related_name='messages', on_delete=models.CASCADE)
    context = models.TextField()
    timestamp = models.DateTimeField(auto_now_add=True)
我也尝试按照本教程进行操作:
https://www.youtube.com/watch?v=xrKKRRC518Y
问候

最佳答案

对于仍然存在此问题的用户:
由于正在使用的 WebSocket 是 AsyncWebsocketConsumer ,我们必须使用 database_sync_to_async . 为什么这样? 因为我们不想留下与数据库的打开连接。
这是你将如何做到的:

@database_sync_to_async
def create_chat(self, msg, sender):
    new_msg = Message.objects.create(sender=sender, msg=msg)
    new_msg.save()
这是一个简单的例子来演示如何成功使用database_sync_to_async :
import json

from channels.db import database_sync_to_async
from channels.generic.websocket import AsyncWebsocketConsumer

from .models import Message


class ChatConsumer(AsyncWebsocketConsumer):
    @database_sync_to_async
    def create_chat(self, msg, sender):
        new_msg = Message.objects.create(sender=sender, msg=msg)
        new_msg.save()
        return new_msg

    async def connect(self):
        self.room_name = self.scope['url_route']['kwargs']['room_name']
        self.room_group_name = 'chat_%s' % self.room_name
        await self.channel_layer.group_add(self.room_group_name, self.channel_name)
        await self.accept()

    async def disconnect(self, close_code):
        await self.channel_layer.group_discard(self.room_group_name, self.channel_name)

    async def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']
        sender = text_data_json['sender']
        await self.channel_layer.group_send(self.room_group_name, {
            'type': 'chat_message',
            'message': message,
            'sender': sender
        })

    async def chat_message(self, event):
        message = event['message']
        sender = event['sender']
        new_msg = await self.create_chat(sender, message)  # It is necessary to await creation of messages
        await self.send(text_data=json.dumps({
            'message': new_msg.message,
            'sender': new_msg.sender
        }))

关于Django channel : Save messages to database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64188904/

相关文章:

python - 如何在 django 上处理 tcp 连接

python - 拉取用户名 - 'ManyRelatedManager' 对象不支持索引

node.js - Phonegap Socket.io 聊天

android - 登录聊天 Quickblox 后获取聊天对话框

python - 如何使用注释和聚合在 Django 的 ORM 中执行此 GROUP BY 查询

python - 打印为 PDF Django 和 ReportLab

python - 无法在heroku中获取媒体文件

django - Django 模型中每个外键的唯一字段

python - 我可以将 Django 模型对象作为单例获取吗?

iphone - 使用 xmppframework 在 iphone 上进行 facebook 聊天