[英]Django getting values from postgres JSON field
I have a simple model like:我有一个简单的 model 像:
class MyModel(models.Model):
data = JSONField()
The JSONField data
is in the following structure: JSONField data
的结构如下:
{
"name": "Brian",
"skills": [
{"id": 4, "name": "First aid"},
{"id": 5, "name": "Second aid"}
]
}
I'd like to create a query that gets a list of MyModels filtered by the id
of the skill
inside the data.我想创建一个查询,以获取由数据中的skill
id
过滤的 MyModels 列表。
I've tried a few different avenues here, and can do the work in Python but I'm pretty sure there's a way to do this in Django;我在这里尝试了几种不同的途径,并且可以在 Python 中完成工作,但我很确定在 Django 中有一种方法可以做到这一点; I think my SQL isn't good enough to figure it out.我认为我的 SQL 不够好,无法弄清楚。
Cheers in advance.提前喝彩。
try this尝试这个
>>> MyModel.objects.filter(data__skills__contains=[{'id':4}, {'id':5}])
more about JSON filter https://docs.djangoproject.com/en/3.1/topics/db/queries/#querying-jsonfield更多关于 JSON 过滤器https://docs.djangoproject.com/en/3.1/topics/db/queries/#querying-jsonfield
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.