'Django send full QuerySet in Channels
I am using Django Channels to build a game. It's a super simple multiplayer chance game. After user clicks a button the game starts and the server receives the data via Channels. I do the necessary calculations, database updates etc. Now I want to send data back to the browser. I do a filter query from the database and I would like to send the whole query back. I know I could take out the necessary data one by one and send them separately but I feel like there's a better way but I just can't figure it out.
When I would send the QuerySet like in views.py I get a TypeError: Object of type QuerySet is not JSON serializable.
Sending the query using list() also gives me an error ValueError: The QuerySet value for an exact lookup must be limited to one result using slicing.
Is there a way I could send the whole QuerySet with Channels?
consumer.py
async def send_message(self, event):
room_name = event['room_name']
roomObj = cbr.objects.filter(room_name=room_name)
cbObj = cb.objects.filter(room=roomObj)
#cbObj is what I would like to send
await self.send(text_data=json.dumps({
'cb': cbObj
# Gives TypeError
}))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
