Tutorial by Examples

Available in Django 1.9+ from django.contrib.postgres.fields import JSONField from django.db import models class IceCream(models.Model): metadata = JSONField() You can add the normal **options if you wish. ! Note that you must put 'django.contrib.postgres' in INSTALLED_APPS in your s...
Pass data in native Python form, for example list, dict, str, None, bool, etc. IceCream.objects.create(metadata={ 'date': '1/1/2016', 'ordered by': 'Jon Skeet', 'buyer': { 'favorite flavor': 'vanilla', 'known for': ['his rep on SO', 'writing a book'] }, ...
IceCream.objects.filter(metadata__ordered_by='Guido Van Rossum')
Get all ice cream cones that were ordered by people liking chocolate: IceCream.objects.filter(metadata__buyer__favorite_flavor='chocolate') See the note in the "Remarks" section about chaining queries.
An integer will be interpreted as an index lookup. IceCream.objects.filter(metadata__buyer__known_for__0='creating stack overflow') See the note in the "Remarks" section about chaining queries.
Ordering directly on JSONField is not yet supported in Django. But it's possible via RawSQL using PostgreSQL functions for jsonb: from django.db.models.expressions import RawSQL RatebookDataEntry.objects.all().order_by(RawSQL("data->>%s", ("json_objects_key",))) This e...

Page 1 of 1