this is an error related to my consumers
class HubriseOrderConsumer(ListModelMixin, GenericAsyncAPIConsumer):
def get_queryset(self, **kwargs):
return HubriseOrder.objects.filter(ascount=self.scope["user"]).exclude(
status='En attente').exclude(
status='Rejeter').exclude(
status='Compléter').exclude(
status='new').exclude(
status='Livraison échouer').order_by('created_at')
serializer_class = HubriseOrderSerializer
permissions = (permissions.AllowAny,)
async def connect(self, **kwargs):
await self.model_change.subscribe()
await super().connect()
Room.objects.add("some_room", self.channel_name, self.scope["user"])
async def disconnect(self, close_code):
Room.objects.remove("some_room", self.channel_name)
@model_observer(HubriseOrder)
async def model_change(self, message, observer=None, **kwargs):
await self.send_json(message)
@model_change.serializer
def model_serialize(self, instance, action, **kwargs):
return dict(data=HubriseOrderSerializer(instance=instance).data, action=action.value)
Refresh your memory regarding database access within async consumers at Database Access — Channels 4.0.0 documentation
I followed the doc but my WebSocket does not want to connect
WebSocket HANDSHAKING /ws/ [127.0.0.1:51431]
WebSocket DISCONNECT /ws/ [127.0.0.1:51431]
Like this 
from channels.db import database_sync_to_async
class HubriseOrderConsumer(ListModelMixin, GenericAsyncAPIConsumer):
@database_sync_to_async
def get_queryset(self, **kwargs):
return HubriseOrder.objects.filter(ascount=self.scope["user"]).exclude(
status='En attente').exclude(
status='Rejeter').exclude(
status='Compléter').exclude(
status='new').exclude(
status='Livraison échouer').order_by('created_at')
serializer_class = HubriseOrderSerializer
permissions = (permissions.AllowAny,)
async def connect(self, **kwargs):
await self.model_change.subscribe()
await super().connect()
Room.objects.add("some_room", self.channel_name, self.scope["user"])
async def disconnect(self, close_code):
Room.objects.remove("some_room", self.channel_name)
@model_observer(HubriseOrder)
async def model_change(self, message, observer=None, **kwargs):
await self.send_json(message)
@model_change.serializer
def model_serialize(self, instance, action, **kwargs):
return dict(data=HubriseOrderSerializer(instance=instance).data, action=action.value)
the decorator, must be in the function that returns the object, right?
@database_sync_to_async
What the docs show (which means I know it works but I don’t know if there is an easier way) is that you need to move your database operations into a function that is decorated with that decorator. You then call that function from wherever. See the structure of the calls at the bottom of that referenced page.
we agree that there is just def get_queryset or there are data operations
Honestly it’s been 2 hours I’m testing a lot of stuff but I have either errors or no results I don’t understand where the error comes from 
All database operations need to be encapsulated in a decorated function.
This also includes “implicit” queries, such as a foreign key reference that results in a query being executed.
here is what seems to me to be the most correct, it displays me more error but my websocket does not connect
HTTP GET /myapp/commande 200 [0.02, 127.0.0.1:58414]
WebSocket HANDSHAKING /ws/ [127.0.0.1:58419]
WebSocket DISCONNECT /ws/ [127.0.0.1:58419]
class HubriseOrderConsumer(ListModelMixin, GenericAsyncAPIConsumer):
@database_sync_to_async
async def connect(self, **kwargs):
await self.model_change.subscribe()
await super().connect()
Room.objects.add("some_room", self.channel_name, self.scope["user"])
# @database_sync_to_async
# async def disconnect(self, close_code):
# Room.objects.remove("some_room", self.channel_name)
@database_sync_to_async
def get_queryset(self, **kwargs):
return HubriseOrder.objects.filter(ascount=self.scope["user"]).exclude(
status='En attente').exclude(
status='Rejeter').exclude(
status='Compléter').exclude(
status='new').exclude(
status='Livraison échouer').order_by('created_at')
serializer_class = HubriseOrderSerializer
permissions = (permissions.AllowAny,)
@model_observer(HubriseOrder)
async def model_change(self, message, observer=None, **kwargs):
await self.send_json(message)
@model_change.serializer
def model_serialize(self, instance, action, **kwargs):
return dict(data=HubriseOrderSerializer(instance=instance).data, action=action.value)
but I added a decorator on this function
Here, no?
@database_sync_to_async
@database_sync_to_async
async def connect(self, **kwargs):
await self.model_change.subscribe()
await super().connect()
Room.objects.add("some_room", self.channel_name, self.scope["user"])
Except the rest of the function is written as an async function.
If you’re going to do it that way, you might as well just make this a synchronous consumer.
I think I’m stupid, I can’t set it up even though I know what to do --’
Sorry for taking up a lot of your time, by the way you have helped me a lot and learned a lot, send me a link so I can buy you a coffee haha.
It’s what I call an “eye for detail” - it’s something you can learn. Start with the basic steps and follow the pattern in the docs exactly.
Don’t just add the decorator to the entire connect function - extract the database operation to a separate function, and decorate it. Then call that new function from connect.
normally it is good ?
class HubriseOrderConsumer(ListModelMixin, GenericAsyncAPIConsumer):
async def connect(self, **kwargs):
await self.model_change.subscribe()
await self.room_connect()
await super().connect()
@database_sync_to_async
def room_connect(self, **kwargs):
Room.objects.add("some_room", self.channel_name, self.scope["user"])
@database_sync_to_async
async def disconnect(self, close_code):
Room.objects.remove("some_room", self.channel_name)
def get_queryset(self, **kwargs):
return HubriseOrder.objects.filter(ascount=self.scope["user"]).exclude(
status='En attente').exclude(
status='Rejeter').exclude(
status='Compléter').exclude(
status='new').exclude(
status='Livraison échouer').order_by('created_at')
serializer_class = HubriseOrderSerializer
permissions = (permissions.AllowAny,)
@model_observer(HubriseOrder)
async def model_change(self, message, observer=None, **kwargs):
await self.send_json(message)
@model_change.serializer
def model_serialize(self, instance, action, **kwargs):
return dict(data=HubriseOrderSerializer(instance=instance).data, action=action.value)
it connects and I see my data but I don’t think it works correctly, because when I add a new data it appears everywhere
Yes, because you haven’t added any logic to your consumer yet to take advantage of this information that you now have available to you.
Just adding the presence module doesn’t resolve the root issue, it merely provides a mechanism that you can use to fix it.