You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django Channels RF V3 @model_observer 偶发message=None排查

Django Channels Rest Framework @model_observer 回调偶发接收message=None问题

问题背景

  • 查阅Django Channels Rest Framework中model observer相关官方文档,未找到@model_observer装饰的回调被触发时传入message=None的相关条件说明。根据官方示例逻辑,message参数应为绑定序列化器的返回值,按理仅当序列化器无返回值时message才会为None。

官方示例代码

class MyConsumer(GenericAsyncAPIConsumer):
    queryset = User.objects.all()
    serializer_class = UserSerializer

    @model_observer(Comments)
    async def comment_activity(
        self,
        message: CommentSerializer,
        observer=None,
        subscribing_request_ids=[]
        **kwargs
    ):
        await self.send_json(message.data)

    @comment_activity.serializer
    def comment_activity(self, instance: Comment, action, **kwargs) -> CommentSerializer:
        '''This will return the comment serializer'''
        return CommentSerializer(instance)

    @action()
    async def subscribe_to_comment_activity(self, request_id, **kwargs):
        await self.comment_activity.subscribe(request_id=request_id)

异常现象

添加日志排查后确认,出现message为None的场景下,序列化器实际已正常接收到模型实例并返回合法序列化结果,且该问题仅偶发出现,不确定是否因对observer内部运行逻辑理解偏差导致。

业务实现代码

class StorageConsumer(ListModelMixin, AsyncAPIConsumer):
    """
    Async API endpoint that allows Storage updates to be pushed.
    """

    async def accept(self, **kwargs):
        await super().accept()
        await self.model_change_bess.subscribe()

    @model_observer(Bess)
    async def model_change_bess(self, message, **kwargs):
        if message is None:
            logger.warning(f"model_change_bess change without a message")
            return

        message["model"] = kwargs["observer"].model_cls.__name__
        await self.send_json(message)

    @model_change_bess.serializer
    def model_serialize(self, instance, action, **kwargs):
        data = BessSerializer(instance).data

        # TODO remove once the message is None bug is fixed
        logger.info(f"model_serialize bess instance: {instance} data: {data}")

        serialized_data = {
            "project_id": data["project"],
            "simulation_id": data["simulation"],
            "data": data,
        }
        return serialized_data

异常对应日志

16/06/2022 13:09:25 [INFO] api_consumers: model_serialize bess instance: Bess object (37227) data: {'id': 37227, 'created': '2022-06-16T13:09:25Z', 'modified': '2022-06-16T13:09:25Z', 'time_stamp': '2022-06-16T13:09:25Z', 'cycles': '0.02541166724274896000000000000000', 'p_bess': '-74.0000', 'soc': '0.45109075850806520000000000000000', 'soh': '0.99514591796314000000000000000000', 'temperature_bess': None, 'time_to_eol': None, 'feedback': True, 'project': 1, 'simulation': 1}
16/06/2022 13:09:25 [WARNING] api_consumers: model_change_bess change without a message

诉求

请熟悉该框架内部机制的开发者提供问题排查思路与解决方案。


内容的提问来源于stack exchange,提问作者Macilias

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.30 04:51:25