Django通过JSONField

时间:2018-09-18 12:41:53

标签: python django django-models

我有Django应用程序和PostgreSQL数据库。 我的问题是我无法按JSONField中名为“ id”的字段进行过滤。

我的模型如下:

class SnapshotAuditing(models.Model):
    data = JSONField(null=True, blank=True)
    created = models.DateTimeField(auto_now_add=True)

我这样填充它:

snapshot = SnapshotAuditing(
    data={
        'id': student.id,
        'some_value': some_value,
    }
)
snapshot.save()

我要执行以下选择请求:

price_change = SnapshotAuditing.objects.filter(
    data__id=student.id
).order_by('created').first()

但是我得到这个错误:

    bl/transfers.py:98: in get_coefficient_and_hours_unused
    data__id=student.id
../env2.7.14/lib/python2.7/site-packages/django/db/models/manager.py:85: in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
../env2.7.14/lib/python2.7/site-packages/django/db/models/query.py:784: in filter
    return self._filter_or_exclude(False, *args, **kwargs)
../env2.7.14/lib/python2.7/site-packages/django/db/models/query.py:802: in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
../env2.7.14/lib/python2.7/site-packages/django/db/models/sql/query.py:1250: in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
../env2.7.14/lib/python2.7/site-packages/django/db/models/sql/query.py:1276: in _add_q
    allow_joins=allow_joins, split_subq=split_subq,
../env2.7.14/lib/python2.7/site-packages/django/db/models/sql/query.py:1210: in build_filter
    condition = self.build_lookup(lookups, col, value)
../env2.7.14/lib/python2.7/site-packages/django/db/models/sql/query.py:1102: in build_lookup
    lhs = self.try_transform(lhs, name, lookups)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <django.db.models.sql.query.Query object at 0x1182f6dd0>, lhs = Col(student_snapshotauditing, student.SnapshotAuditing.data), name = 'id', rest_of_lookups = ['id']

    def try_transform(self, lhs, name, rest_of_lookups):
        """
            Helper method for build_lookup. Tries to fetch and initialize
            a transform for name parameter from lhs.
            """
        transform_class = lhs.get_transform(name)
        if transform_class:
            return transform_class(lhs)
        else:
            raise FieldError(
                "Unsupported lookup '%s' for %s or join on the field not "
                "permitted." %
>               (name, lhs.output_field.__class__.__name__))
E           FieldError: Unsupported lookup 'id' for JSONField or join on the field not permitted.

../env2.7.14/lib/python2.7/site-packages/django/db/models/sql/query.py:1120: FieldError

请帮忙吗?

0 个答案:

没有答案