Skip to main content

Django Postgres JSONB Fields support with lookups

Project description

# django-pgjsonb
Django Postgres JSONB Fields support with lookups

Originaly inpired by [django-postgres](https://bitbucket.org/schinckel/django-postgres/)

Install
=======
pip install django-pgjsonb

Use
===
from django_pgjsonb import JSONField
class Article(models.Model):
meta=JSONField([null=True,default={}])

Lookups
=======
###Contains wide range of lookups support natively by postgres

1. `has` :if field has specific key *`("?")`*

Artile.objects.filter(meta__has="author")
2. `has_any` : if field has any of the specific keys *`("?|")`*

Artile.objects.filter(meta__has_any=["author","date])
3. `has_all` : if field has all of the specific keys *`("?&")`*

Artile.objects.filter(meta__has_all=["author","date])
4. `contains` : if field contains the specific keys and values *`("@>")`*

Article.objects.filter(meta__contains={"author":"yjmade","date":"2014-12-13"})
5. `in` or `contained_by` : if all field key and value contain by input *`("<@")`*

Artile.objects.filter(meta__in={"author":"yjmade","date":"2014-12-13"})
6. `len` : the length of the array ,transform to int,and can followed int lookup like gt or lt *`("jsonb_array_length()")`*

Artile.objects.filter(meta__authors__len__gte=3)
Article.objects.filter(meta__authors__len=10)
7. `as_(text,int,float,bool,date,datetime)` : transform json field into sepcific data type so that you can follow operation of this type *`("CAST(FIELD as TYPE)")`*

Article.objects.filter(meta__date__as_datetime__year__range=(2012,2015))
Article.objects.filter(meta__view_count__as_float__gt=100)
Article.objects.filter(meta__title__as_text__iregex=r"^\d{4}")
8. `path_(PATH)` : get the specific path, path split by '_' *`("#>")`*

Article.objects.filter(meta__path_author_articles__contains="show me the money")

Add function to QuerySet
========================
1.`select_json(field_name="JSON_PATHS")`

JSON_PATHS in format of path seperate by "__",like "meta__location__geo_info".it will use queryset extra method to transform a value inside json as a field.

Article.objects.select_json(geo="meta__location__geo_info")`
This operation will translate to sql as

SELECT "meta"->'location'->>'geo_info' as "geo"
After select_json ,then the field_name can be operate in values() and values_list() method,so that

1. select only one sepecific value inside json
2. to group by one value inside json

is possible.

Demo:

1.Article.objects.all().select_json(tags="meta__tags").values_list("tags")
#select only "meta"->'tags'
2.Article.objects.all().select_json(author_name="meta__author__name")
.values("author_name").annotate(count=models.Count("author_name"))
#GROUP BY "meta"->'author'->'name'




#####For more infomation about raw jsonb operation,please see [PostgreSQL Document](http://www.postgresql.org/docs/9.4/static/functions-json.html)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-pgjsonb-0.0.5.tar.gz (5.0 kB view details)

Uploaded Source

File details

Details for the file django-pgjsonb-0.0.5.tar.gz.

File metadata

File hashes

Hashes for django-pgjsonb-0.0.5.tar.gz
Algorithm Hash digest
SHA256 edafae9718b6d1ed911c4683aba635586574e0168813f707806cde37fc63f3ee
MD5 7844edff52e308b5eddd962d07563356
BLAKE2b-256 0ab45c2640cf79c57768f3f90fc385f74035f8f60d1e3d3626f41999b27b2fe3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page