Nea*_*ara 7 python django orm django-models django-jsonfield
我正在使用Django 1.5.4和PostgreSQL 9.3,使用django-jsonfield进行JSONField.
以下查询抛出db错误(无法识别类型json的相等运算符):
ModelWithJsonField.objects.annotate(count=Count('field_to_count_by'))
Run Code Online (Sandbox Code Playgroud)
这field_to_count_by不是JSONField,普通的int字段.
任何想法如何我可以解决问题,仍然使用注释?
引擎盖后面有什么注释?
我遇到了同样的问题,最后(今天)通过在psql控制台中以管理员身份运行它来实现一个假操作符:
-- This creates a function named hashjson that transforms the
-- json to texts and generates a hash
CREATE OR REPLACE FUNCTION hashjson(
json
) RETURNS INTEGER LANGUAGE SQL STRICT IMMUTABLE AS $$
SELECT hashtext($1::text);
$$;
-- This creates a function named json_eq that checks equality (as text)
CREATE OR REPLACE FUNCTION json_eq(
json,
json
) RETURNS BOOLEAN LANGUAGE SQL STRICT IMMUTABLE AS $$
SELECT bttextcmp($1::text, $2::text) = 0;
$$;
-- This creates an operator from the equality function
CREATE OPERATOR = (
LEFTARG = json,
RIGHTARG = json,
PROCEDURE = json_eq
);
-- Finaly, this defines a new default JSON operator family with the
-- operators and functions we just defined.
CREATE OPERATOR CLASS json_ops
DEFAULT FOR TYPE json USING hash AS
OPERATOR 1 =,
FUNCTION 1 hashjson(json);
Run Code Online (Sandbox Code Playgroud)
(深受这个帖子的启发)
我还在django-jsonfield GitHub 问题中引用了你的问题.
注意 :
| 归档时间: |
|
| 查看次数: |
2372 次 |
| 最近记录: |