在 Django 通道中查询模型
Querying Models in Django Channels
我一直在尝试从我的 consumers.py 查询我的数据库,但似乎查询不起作用,因为我不断收到此错误,表明查询未发送。这是错误:
File "/Users/MichaelAjanaku/Desktop/Kitchen-Order/order/lib/python3.6/site-packages/channels/utils.py", line 51, in await_many_dispatch
await dispatch(result)
File "/Users/MichaelAjanaku/Desktop/Kitchen-Order/order/lib/python3.6/site-packages/channels/consumer.py", line 73, in dispatch
await handler(message)
File "/Users/MichaelAjanaku/Desktop/Kitchen-Order/order/lib/python3.6/site-packages/channels/generic/websocket.py", line 196, in websocket_receive
await self.receive(text_data=message["text"])
File "/Users/MichaelAjanaku/Desktop/Kitchen-Order/Kitchen_Order/Order_app/consumers.py", line 52, in receive
info = order_data_json['info']
KeyError: 'info'
WebSocket DISCONNECT /ws/orders/ [127.0.0.1:53953]
这是我的 consumers.py 设置:
import json
from .models import Order
from channels.db import database_sync_to_async
from channels.generic.websocket import AsyncWebsocketConsumer
class WSConsumer(AsyncWebsocketConsumer):
@database_sync_to_async
def _get_order_info(self, number):
return Order.objects.get(order_number = number)
async def connect(self):
self.groupname = 'kitchen'
await self.channel_layer.group_add(
self.groupname,
self.channel_name,
)
await self.accept()
async def disconnect(self, code):
await self.channel_layer.group_discard(
self.groupname,
self.channel_name,
)
await super().disconnect(code)
async def take_order(self, event):
number = event['number']
details = event['details']
info = await self._get_order_info(number)
await self.send(text_data=json.dumps({
'number' : number,
'details' : details,
'info' : info,
}))
async def receive(self, text_data):
order_data_json = json.loads(text_data)
number = order_data_json['number']
details = order_data_json['details']
info = order_data_json['info']
await self.channel_layer.group_send(
self.groupname,
{
'type': 'take_order',
'number' : number,
'details' : details,
'info' : info,
}
)
前端Websocket代码:
document.querySelector('#submit').onclick = function(e) {
const numberInputDom = document.querySelector('#id_order_number');
const detailsInputDom = document.querySelector('#id_order_details');
const number = numberInputDom.value;
const details = detailsInputDom.value;
orderSocket.send(JSON.stringify({
'number': number,
'details' : details,
}));
console.log('data sent')
};
所以基本上我是从模板中的表单发送数据,然后我想根据我的数据库查询该数据,以获取数据的完整信息,但遗憾的是,我的查询不起作用。
我已经尝试了我所知道的一切,但都无济于事。
可能出了什么问题或我错过了什么?谢谢
我认为这不是与数据库查询相关的问题,而是与您的 WS 在 receive
方法中接收的数据有关的问题。您在消息中发送 number
和 details
,但是 js 代码中您的 JSON 对象中的 info
字段在哪里?
receive
方法中的 KeyError: 'info'
意味着您正试图访问 order_data_json
词典中不存在的键。尝试将info
添加到前端的JSON,发送消息并再次尝试!
我一直在尝试从我的 consumers.py 查询我的数据库,但似乎查询不起作用,因为我不断收到此错误,表明查询未发送。这是错误:
File "/Users/MichaelAjanaku/Desktop/Kitchen-Order/order/lib/python3.6/site-packages/channels/utils.py", line 51, in await_many_dispatch
await dispatch(result)
File "/Users/MichaelAjanaku/Desktop/Kitchen-Order/order/lib/python3.6/site-packages/channels/consumer.py", line 73, in dispatch
await handler(message)
File "/Users/MichaelAjanaku/Desktop/Kitchen-Order/order/lib/python3.6/site-packages/channels/generic/websocket.py", line 196, in websocket_receive
await self.receive(text_data=message["text"])
File "/Users/MichaelAjanaku/Desktop/Kitchen-Order/Kitchen_Order/Order_app/consumers.py", line 52, in receive
info = order_data_json['info']
KeyError: 'info'
WebSocket DISCONNECT /ws/orders/ [127.0.0.1:53953]
这是我的 consumers.py 设置:
import json
from .models import Order
from channels.db import database_sync_to_async
from channels.generic.websocket import AsyncWebsocketConsumer
class WSConsumer(AsyncWebsocketConsumer):
@database_sync_to_async
def _get_order_info(self, number):
return Order.objects.get(order_number = number)
async def connect(self):
self.groupname = 'kitchen'
await self.channel_layer.group_add(
self.groupname,
self.channel_name,
)
await self.accept()
async def disconnect(self, code):
await self.channel_layer.group_discard(
self.groupname,
self.channel_name,
)
await super().disconnect(code)
async def take_order(self, event):
number = event['number']
details = event['details']
info = await self._get_order_info(number)
await self.send(text_data=json.dumps({
'number' : number,
'details' : details,
'info' : info,
}))
async def receive(self, text_data):
order_data_json = json.loads(text_data)
number = order_data_json['number']
details = order_data_json['details']
info = order_data_json['info']
await self.channel_layer.group_send(
self.groupname,
{
'type': 'take_order',
'number' : number,
'details' : details,
'info' : info,
}
)
前端Websocket代码:
document.querySelector('#submit').onclick = function(e) {
const numberInputDom = document.querySelector('#id_order_number');
const detailsInputDom = document.querySelector('#id_order_details');
const number = numberInputDom.value;
const details = detailsInputDom.value;
orderSocket.send(JSON.stringify({
'number': number,
'details' : details,
}));
console.log('data sent')
};
所以基本上我是从模板中的表单发送数据,然后我想根据我的数据库查询该数据,以获取数据的完整信息,但遗憾的是,我的查询不起作用。
我已经尝试了我所知道的一切,但都无济于事。
可能出了什么问题或我错过了什么?谢谢
我认为这不是与数据库查询相关的问题,而是与您的 WS 在 receive
方法中接收的数据有关的问题。您在消息中发送 number
和 details
,但是 js 代码中您的 JSON 对象中的 info
字段在哪里?
receive
方法中的 KeyError: 'info'
意味着您正试图访问 order_data_json
词典中不存在的键。尝试将info
添加到前端的JSON,发送消息并再次尝试!