在 Mongoengin 中获取弱引用错误

问题描述 投票:0回答:1

我在 django 应用程序中使用 mongoengin。 mongo 中的每个文档都有 dict 元数据,其中有不带 base_url 的图像地址。 在 django 序列化器中,我向其中添加了 base_url 但有时会出现错误

    def to_representation(self, instance):
        # when we search by image result has similarity and it`s milvus similarity and when search by text,
        # we show it`s reference similarity
        # obj.similarity: we had image and have similarity from milvus.
        # obj.metadata.get('similarity') :we don`t have similarity from milvus use matched item similarity.
        data = super(ResultSerializer, self).to_representation(instance)
        if instance.similarity is not None:
            data['similarity'] = decimal.Decimal(distance_to_score(instance.similarity)).quantize(
                decimal.Decimal('0.000'), rounding=decimal.ROUND_DOWN)
        else:
            data['similarity'] = instance.metadata.get('similarity')

        if frame_url := instance.metadata.get('frame_url'):
            data.get('metadata')['frame_url'] = SHAHIN_SETTING['base_url'] + frame_url
        return data

错误:

Jan 23 16:17:10.527 | develop-shahin-develop-86df584cf6-wtrcm | paginated_data['results'] = ResultSerializer(el_result, many=True).data
Jan 23 16:17:10.527 | develop-shahin-develop-86df584cf6-wtrcm | File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 745, in data
Jan 23 16:17:10.527 | develop-shahin-develop-86df584cf6-wtrcm | ret = super().data
Jan 23 16:17:10.527 | develop-shahin-develop-86df584cf6-wtrcm | File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 246, in data
Jan 23 16:17:10.527 | develop-shahin-develop-86df584cf6-wtrcm | self._data = self.to_representation(self.instance)
Jan 23 16:17:10.527 | develop-shahin-develop-86df584cf6-wtrcm | File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 663, in to_representation
Jan 23 16:17:10.527 | develop-shahin-develop-86df584cf6-wtrcm | return [
Jan 23 16:17:10.527 | develop-shahin-develop-86df584cf6-wtrcm | File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 664, in <listcomp>
Jan 23 16:17:10.527 | develop-shahin-develop-86df584cf6-wtrcm | self.child.to_representation(item) for item in iterable
Jan 23 16:17:10.527 | develop-shahin-develop-86df584cf6-wtrcm | File "/app/apps/search/serializers/v2.py", line 67, in to_representation
Jan 23 16:17:10.527 | develop-shahin-develop-86df584cf6-wtrcm | data.get('metadata')['frame_url'] = SHAHIN_SETTING['base_url'] + frame_url
Jan 23 16:17:10.527 | develop-shahin-develop-86df584cf6-wtrcm | File "/usr/local/lib/python3.8/site-packages/mongoengine/base/datastructures.py", line 35, in wrapper
Jan 23 16:17:10.528 | develop-shahin-develop-86df584cf6-wtrcm | self._mark_as_changed(key)
Jan 23 16:17:10.528 | develop-shahin-develop-86df584cf6-wtrcm | File "/usr/local/lib/python3.8/site-packages/mongoengine/base/datastructures.py", line 98, in _mark_as_changed
Jan 23 16:17:10.528 | develop-shahin-develop-86df584cf6-wtrcm | if hasattr(self._instance, "_mark_as_changed"):
Jan 23 16:17:10.528 | develop-shahin-develop-86df584cf6-wtrcm | ReferenceError: weakly-referenced object no longer exists
python django django-serializer mongoengine
1个回答
0
投票

经过一番查找和追踪,发现这个错误是因为我想更改并返回

mongoengine.base.datastructs.BaseDict

我将其更改为Python Dict并修复了错误。

© www.soinside.com 2019 - 2024. All rights reserved.