0

使用 jsonfield“包含”查询时,根据后端数据库,我得到的结果不一致。

from polymorphic.models import PolymorphicModel
import jsonfield

class Action(PolymorphicModel):
    new_object = jsonfield.JSONField(null=True)

在另一个模型中,我过滤了这个 jsonfield,但我得到了不同的结果。

在 sqlilite 上,以下工作:

    return Action.objects.filter(
            new_object__contains={"ref_id": self.id}
        ).order_by("-created_at")[:5]

在 mysql 上,我必须执行以下操作:

    return Action.objects.filter(
            new_object__contains=f"\"ref_id\": {self.id}"
        ).order_by("-created_at")[:5]

因此,在我看来,在一种环境中,它将 json 反序列化为字典,而另一种则将其保留为字符串。

有没有人有处理这个的好方法?是否存在与数据库不匹配的配置之一的问题?

编辑:这是 Django 2.2

4

0 回答 0