gpt4 book ai didi

Django channel : Save messages to database

转载 作者:行者123 更新时间:2023-12-04 13:11:58 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com