postgres.fields.JSONField

class JSONField(**options) [source]

A field for storing JSON encoded data. In Python the data is represented in its Python native format: dictionaries, lists, strings, numbers, booleans and None.

If you want to store other data types, you’ll need to serialize them first. For example, you might cast a datetime to a string. You might also want to convert the string back to a datetime when you retrieve the data from the database. There are some third-party JSONField implementations which do this sort of thing automatically.

If you give the field a default, ensure it’s a callable such as dict (for an empty default) or a callable that returns a dict (such as a function). Incorrectly using default={} creates a mutable default that is shared between all instances of JSONField.

Note

PostgreSQL has two native JSON based data types: json and jsonb. The main difference between them is how they are stored and how they can be queried. PostgreSQL’s json field is stored as the original string representation of the JSON and must be decoded on the fly when queried based on keys. The jsonb field is stored based on the actual structure of the JSON which allows indexing. The trade-off is a small additional cost on writing to the jsonb field. JSONField uses jsonb.

As a result, this field requires PostgreSQL ≥ 9.4 and Psycopg2 ≥ 2.5.4.

doc_Django
2016-10-09 18:39:18
Comments
Leave a Comment

Please login to continue.