Python 如何在django通道rest框架中订阅模型的所有实例?
我想将API的行为更改为JSON触发(从浏览器调用),但由于对Python的了解有限,我甚至无法从Python客户端调用它 有人能帮我做些什么吗?这是我的简单客户:Python 如何在django通道rest框架中订阅模型的所有实例?,python,django-rest-framework,django-channels,Python,Django Rest Framework,Django Channels,我想将API的行为更改为JSON触发(从浏览器调用),但由于对Python的了解有限,我甚至无法从Python客户端调用它 有人能帮我做些什么吗?这是我的简单客户: class GenericAsyncAPIConsumerWith(GenericAsyncAPIConsumer): async def websocket_connect(self, message): # Super Save await super().websocket_conne
class GenericAsyncAPIConsumerWith(GenericAsyncAPIConsumer):
async def websocket_connect(self, message):
# Super Save
await super().websocket_connect(message)
# Initialized operation
await self.model_activity.subscribe()
class UserConsumer(ObserverModelInstanceMixin, GenericAsyncAPIConsumerWith):
queryset = Course.objects.order_by("-start_time")
serializer_class = UserSerializer
# permission_classes = [IsAuthenticated]
@model_observer(User)
async def model_activity(self, message, observer=None, **kwargs):
# send activity to your frontend
await self.send_json(message)
我觉得文档有点不清楚,这是解决方案,也做了公关
class ModelConsumerObserver(AsyncAPIConsumer):
async def accept(self, **kwargs):
await super().accept()
await self.model_change.subscribe()
@model_observer(models.Test)
async def model_change(self, message, **kwargs):
await self.send_json(message)
从那时起,websocket将把模型更改推送到客户端这样做时,我不会收到任何关于更改的信息。我成功地连接到路由,但当我修改产品时,我没有从服务器收到任何到客户端的消息 这是我的消费者:
class ProductConsumer(AsyncAPIConsumer):
async def accept(self, **kwargs):
await super().accept(** kwargs)
await self.model_change.subscribe()
@model_observer(Product)
async def model_change(self, message, **kwargs):
await self.send_json(message)
请提供您是否检查服务器上的任何异常?什么是authen方法?