我想将 API 的行为更改为 JSON 触发(从浏览器调用),但由于我对 Python 的了解有限,我什至无法从 Python 客户端调用它。
有人可以帮我怎么做吗manual https://github.com/hishnash/djangochannelsrestframework#subscribing-to-all-instances-of-a-model显示?这是我的简单客户端:
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 会将模型更改推送给客户端
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)