如何判断一个元素是否在定义的Jsonfield列表中?
How to judge if an element is in list as defined Jsonfield?
我使用与 exsits 相关的 peewee table:
import peewee
from playhouse.postgres_ext import *
class Rules(peewee.Model):
channels = JSONField(null=True)
remark = peewee.CharField(max_length=500, null=True)
class Meta:
database = db
db_table = 'biz_rule'
schema = 'opr'
示例:在我的 table 列通道中存在一条记录:
["A012102","C012102","D012102","E012102"]
我想判断"A012102"是否在列表中,代码怎么写?
如果您使用的是 PostgreSQL 9.4+,则可以使用相应的 postgres_ext.BinaryJSONField
peewee 字段类型来使用 jsonb
数据类型。它具有对应于 PostgreSQL ?|
和 ?&
运算符 (see the PostgreSQL JSON docs) 的 contains_any()
和 contains_all()
方法。所以我认为应该是这样的:
from playhouse.postgres_ext import BinaryJSONField
class Rules(peewee.Model):
channels = BinaryJSONField(null=True)
...
query = Rules.select().where(Rules.channels.contains_all('A012102'))
我使用与 exsits 相关的 peewee table:
import peewee
from playhouse.postgres_ext import *
class Rules(peewee.Model):
channels = JSONField(null=True)
remark = peewee.CharField(max_length=500, null=True)
class Meta:
database = db
db_table = 'biz_rule'
schema = 'opr'
示例:在我的 table 列通道中存在一条记录:
["A012102","C012102","D012102","E012102"]
我想判断"A012102"是否在列表中,代码怎么写?
如果您使用的是 PostgreSQL 9.4+,则可以使用相应的 postgres_ext.BinaryJSONField
peewee 字段类型来使用 jsonb
数据类型。它具有对应于 PostgreSQL ?|
和 ?&
运算符 (see the PostgreSQL JSON docs) 的 contains_any()
和 contains_all()
方法。所以我认为应该是这样的:
from playhouse.postgres_ext import BinaryJSONField
class Rules(peewee.Model):
channels = BinaryJSONField(null=True)
...
query = Rules.select().where(Rules.channels.contains_all('A012102'))