Django 异步模型更新尚未颁布

发布于 2025-01-16 07:58:29 字数 1427 浏览 1 评论 0原文

SQL 更新似乎没有被执行,也没有抛出任何错误。下面是我的代码的简化版本。对于上下文,模型中的“选择”字段是一个布尔字段,默认值为 False,并且用户(理想情况下)可以通过发送带有“CHOICE”事件和“Yes”消息的 JSON 包来更改此设置。

consumers.py

import json
from channels.generic.websocket import AsyncJsonWebsocketConsumer
from asgiref.sync import sync_to_async
from .models import Room

class Consumer(AsyncJsonWebsocketConsumer):
    async def connect(self):
        self.room_code = self.scope['url_route']['kwargs']['room_code']
        #Websockets connection code
    async def disconnect(self):
        #Websockets disconnect code
    async def receive(self, text_data):
        response = json.loads(text_data)
        event = response.get("event", None)
        message = response.get("message", None)
        if event == "CHOICE":
            room_set = await sync_to_async(Room.objects.filter)(room_code=self.room_code)
            room = await sync_to_async(room_set.first)()
            if (not room.choice) and message["choice"] == 'Yes':
                sync_to_async(room_set.update)(choice=True) #line seems to not be working
            elif room.choice and message["choice"] == 'No':
                sync_to_async(room_set.update)(choice=False)
            #code to send message to group over Websockets
        #code regarding other events
    async def send_message(self, res):
        #Websockets send message code

我尝试在此处仅包含相关代码,但如果需要更多代码,请告诉我。提前致谢!

The SQL update does not seem to be being enacted upon, and no errors are being thrown either. Below is a simplified version of my code. For context, the "choice" field in the model is a Boolean Field with a default of False, and a user may (ideally) change this by sending a JSON package with the "CHOICE" event and "Yes" message.

consumers.py

import json
from channels.generic.websocket import AsyncJsonWebsocketConsumer
from asgiref.sync import sync_to_async
from .models import Room

class Consumer(AsyncJsonWebsocketConsumer):
    async def connect(self):
        self.room_code = self.scope['url_route']['kwargs']['room_code']
        #Websockets connection code
    async def disconnect(self):
        #Websockets disconnect code
    async def receive(self, text_data):
        response = json.loads(text_data)
        event = response.get("event", None)
        message = response.get("message", None)
        if event == "CHOICE":
            room_set = await sync_to_async(Room.objects.filter)(room_code=self.room_code)
            room = await sync_to_async(room_set.first)()
            if (not room.choice) and message["choice"] == 'Yes':
                sync_to_async(room_set.update)(choice=True) #line seems to not be working
            elif room.choice and message["choice"] == 'No':
                sync_to_async(room_set.update)(choice=False)
            #code to send message to group over Websockets
        #code regarding other events
    async def send_message(self, res):
        #Websockets send message code

I've tried to only include the relevant code here, but if more is needed please let me know. Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

累赘 2025-01-23 07:58:29

我通过在 sync_to_async(room.update)(choice=True) 行之前添加 await 解决了此问题。似乎没有 await 它会在完成 SQL 更新之前移动到下一行代码,导致更新无法完成。

I fixed this issue by adding await before the sync_to_async(room.update)(choice=True) lines. It seems without an await it will move onto the next line of code before completing the SQL update, causing the update to not go through.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文