django postgres jsonb字段支持查找

django-pgjsonb的Python项目详细描述


#django pgjsonb
django postgres jsonb字段支持查找

9-11:0.0.28
fix-contain-contain-contain-contain-contain-空{}
fix-error-of-has-any/has-all
fix lookup ``过滤器(meta={})``

2017-08-31:0.0.27
fix{}python3的python3的查找


2017-08-31:0.0.0.0.26
fix-has-lookup-adjango 1.10




2017-2017-05-05-05-18:0.0.
删除索引且索引已删除时抑制异常。


2017-03-14:0.0.24
使用Postgres EarthDistance插件添加对"近距离查找"的支持,这要感谢@steinliber

<2016-06-01:0.0.23
select-json的fix值未从json中解码0.0.18

2016-03-24:0.0.22
fix error 11删除未预期的解码浮点到十进制

2016-03-19:0.0.21
fix error 10


2016-03-09:0.0.20
添加select json的数组长度

2016-03-08:0.0.19
无法生成创建索引sql

2016-03-01:0.0.18
我们希望能够使用自定义解码器加载json,因此请避免psycopg2的解码json,只需返回原始文本,然后根据字段从数据库值进行配置

2016-03-01:0.0.17
修补django serializer不返回字符串化结果

<2015-07-23:0.0.16
添加对./manage.py inspectdb的支持```python
from django_pgjsonb import jsonfield


class article(models.model):
meta=jsonfield([null=true,default={},decode_kwargs={},encode_kwargs={},db_index=false,db_index_options={}])
````


编码器和解码器选项
==
rgs您可以使用自定义的json转储和加载行为,基本上这些参数只传递给json.loads(**decode_args)和json.dumps(**encode_args)

导入ejson

类文章(models.model):
meta=jsonfield(encode_kwargs={"cls":ejson.ejsonencoder},decode_kwargs={"cls":ejson.ejsondecoder})
````


add index
===
[新的外接程序0.0.15]

jsonb字段支持gin type索引到加速器过滤。由于json是一个包含层次结构的数据结构,因此jsonb字段的索引将比另一个单独的值字段更复杂。更多信息,请参考[Postgres文档8.14.4](http://www.postgresql.org/docs/9.4/static/datatype json.html)

``python
meta=jsonfield(db_index=true)

meta=jsonfield(db_index=true,db_index=options={"path":"作者姓名","仅包含":true})

meta=jsonfield(db_index=true,db_index_options=[{},{"path":"authors_u name","only_u contains":true}]
```

索引将作为非默认的gin运算符类jsonb_path_ops,它只支持索引``包含``运算符,但占用的空间更少,效率更高。

当在db_index_options中指定path参数`` db_index_options={"path":"authors_u name"}``时,索引将生成到指定的路径,以便"article.objects.filter(meta-authors\uu name\uu contains=["asd"])``可以利用索引。

将生成多个对应索引。空的dict代表默认的gin索引。



lookups
==
` has`:如果字段具有特定键*`("?")`*

``python
article.objects.filter(meta_u has="author")
````

2.` has_any`:如果字段有任何特定键*`("?|")`*

`` python
article.objects.filter(meta_u has_u any=["author","date"])
```
3.` has_all`:如果字段具有所有特定键*`("?&;")`*

``python
article.objects.filter(meta\u has\u all=["author","date"])
```
4.`包含`:if字段包含特定的键和值`(@>;)`*
``python
article.objects.filter(meta\uu contains={"author":"yjmade","date":"2014-12-13"})
```

5。` in`or`contained嫒by`:如果所有字段键和值都包含by input*`("<;@")`*
``python
article.objects.filter(meta嫒in={"author":"yjmade","date":"2014-12-13"})
```

6。` len`:数组的长度,转换为int,可以像gt或lt*`("jsonb_array_length()")`*

``python
article.objects.filter(meta_u authors_uu len_u gte=3)
article.objects.filter(meta_u authors_u len=10)
```
7那样遵循int查找。` as(text,int,float,bool,date,datetime)`:将json字段转换为特定的数据类型,以便可以执行此类型的操作*`("cast(field as type)")`*

`` python
`` article.objects.filter(meta_date_as_datetime_year_range=(20122015))
article.objects.filter(meta_view_计数为浮点值(100)
article.objects.filter(元标题(meta)为文本值(r)^\d{4}")
````
8。`路径(path)`:获取具体路径,路径被‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` _名称="json路径")`

json路径的格式是由"\uu"分隔的路径,如"meta\uu location\uu geo\u info"。它将使用QuerySet的"extra"方法将JSON中的值转换为字段。
如果没有提供字段名,它将生成一个字段名,其中的查找用不带JSON字段自身名称的分隔符隔开,因此"select"JSON("meta"author")"等于"select"JSON(author"meta")authoor_u name")`

``python
article.objects.select_json("meta_u author_u name",geo="meta_u location_u geo_u info")`
`````

此操作将转换为SQL,即将"geo"、"article"、"meta"->;"author"->;"location"->;"geo_info"转换为"geo"、"article"、"meta"->;"author"->;"name"'作为"作者名"
``

[new add-in 0.0.20]
```

select_json之后,字段名可以在values()和values_list()方法中操作,这样

1。在json中只选择一个特定值
2。在json中按一个值分组是可能的。


演示:

``python
article.objects.all()。选择"json(tags="meta_u tags").values_list("tags")
仅选择"meta"->;"tags"

article.objects.all()。选择"json(author_name="meta_author_u name")\
.values("auth或"名称").annotate(count=models.count("author_name"))
按"meta"分组-->;"author"->;"name"
`````







支持jsonb中的地理搜索
===
**需要**:postgresql插件:

1。立方体

2.接地距离

3。要安装这两个插件,请在psql中运行下面的命令```json
{"location":[30.2199.4]}只需保留一个纬度、经度列表
`````

demo

``python
```article.objects.filter(data_u location_u near=[39.9,116.45000])纬度、经度、搜索范围
````

``python
``article.objects.filter(data_lo阳离子"near="39.9116.45000")纬度、经度、搜索范围
````

**警告**:如果不传递确切的参数数,则不会使用此筛选器

**有关更多地球距离**,请参阅[PostgreSQL地球距离文档](https://www.postgresql.org/docs/8.3/static/earthdistance.html)


——HTML)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java创建猜谜游戏程序   JavaWebSocketContainer。connectToServer似乎挂起了   如何在java中中断函数   java c#socket client multiple BeginSend()未到达服务器   不可见的组件然后在Java中的窗格之间切换   java在应用程序类中使用静态接口安全吗?   java等待函数完成,直到回调到来   使用DataOutputStream时的java新行,Android   java服务对象的定义是什么?   基于视图的javahibernate复合密钥   java将varchar连接到char在JPA(oracle)中不起作用   如何在java中通过point类读取多个点?