Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将消息保存到consumers.py中的数据库_Python_Django - Fatal编程技术网

Python 如何将消息保存到consumers.py中的数据库

Python 如何将消息保存到consumers.py中的数据库,python,django,Python,Django,我按照频道文档创建了一个聊天应用程序。我已经创建了一个消息模型和一个模型表单,但是当我键入消息并单击“发送”时,它会相应地响应,但消息不会保存到数据库中。我希望在单击“发送”时保存邮件。 我相信我应该在consumers.py中编写一个save方法,但我不确定如何做到这一点 consumers.py from asgiref.sync import async_to_sync from channels.generic.websocket import WebsocketConsumer fr

我按照频道文档创建了一个聊天应用程序。我已经创建了一个消息模型和一个模型表单,但是当我键入消息并单击“发送”时,它会相应地响应,但消息不会保存到数据库中。我希望在单击“发送”时保存邮件。 我相信我应该在consumers.py中编写一个save方法,但我不确定如何做到这一点

consumers.py

from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer

from .models import Message


class ChatConsumer(WebsocketConsumer):
    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
        async_to_sync(self.channel_layer.group_add)(
            self.room_group_name,
            self.channel_name
        )
        self.accept()

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

    # Receive message from WebSocket
    def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']
        # Send message to room group
        async_to_sync(self.channel_layer.group_send)(
            self.room_group_name,
            {
                'type': 'chat_message',
                'message': message
            }
        )

    # Receive message from room group
    def chat_message(self, event):
        message = event['message']
        # Send message to WebSocket
        self.send(text_data=json.dumps({
            'message': message
                                        }))

    def save_message(self):
        ???

如果您需要将来自消费者的消息保存到数据库中,那么我认为您可以使用receive方法:

def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']
        message = Message.objects.create(content=message, user=self.scope["user"], room=self.room_name)
        ...

@AyzayPrince如果此问题解决了您的问题,请确保将其标记为已接受。