File: README.rst

package info (click to toggle)
python-django-jsonfield 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 204 kB
  • sloc: python: 568; makefile: 6
file content (306 lines) | stat: -rw-r--r-- 8,898 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
django-jsonfield
================

**Maintenance mode only:** It is not recommended you use this library on new
projects. See the (long) **History** section below for why and alternatives.

----

Cross-database JSON field for Django models.

History
-------


This project was created in 2010 by Matthew Schinckel. He created it based upon
code from `David Cramer's
blog <https://web.archive.org/web/20140731084522/http://cramer.io/2009/04/14/cleaning-up-with-json-and-sql/>`_,
had the repository in Mercurial on
`BitBucket <https://bitbucket.org/schinckel/django-jsonfield>`_, and
maintained it until 2018. In March 2019, Adam Johnson took over maintenance
(from an invite back in 2018!), and moved it to Git on
`GitHub <https://github.com/adamchainz/django-jsonfield>`_ because he's no good
at Mercurial and "everyone" uses GitHub these days.

At the time it was created, the databases that Django supports didn't feature
native JSON support. Since then, most of them have gained that ability.
Correspondingly, there are some Django field implementations for taking
advantage of this:

* A PostgreSQL ``JSONField`` is provided in
  `django.contrib.postgres <https://docs.djangoproject.com/en/2.1/ref/contrib/postgres/fields/>`_,
  which was created as part of Django in version 1.9, released December 2015.
  Note this library interferes with the way that works, see
  `issue 5 <https://github.com/adamchainz/django-jsonfield/issues/5>`_ for
  explanation and a workaround.
* A MySQL (and maybe MariaDB) ``JSONField`` is provided in
  `Django-MySQL <https://django-mysql.readthedocs.io/en/latest/model_fields/json_field.html>`_,
  since version 1.0.7, released March 2016.

At time of writing this history (March 2019), there still isn't a JSONField
implementation that can take advantage of the native features on all the
databases. This has been discussed on the ``django-developers`` mailing list
several times though.

The ``JSONField`` provided by this library uses native features on
PostgreSQL, but not on any other database, so it's in a bit of a weird place.

If you are considering adding this to a new project, you probably don't want
it, instead:

* If you want native JSON support from your database and you're using
  PostgreSQL or MySQL, use the native fields as per the links above.
* If you don't want native JSON support, consider just storing the JSON in a
  ``TextField`` and deserializing it appropriately in your code, perhaps with
  a simple model property to proxy it.
* If you need native JSON support on a database for which there is no Django
  field implementation, try making it yourself or getting in touch to see if
  there's something that can be done.

Installation
------------

Install it with **pip**:

.. code-block:: sh

    pip install django-jsonfield

Then use the field in your models:

.. code-block:: python

    from django.db import models
    import jsonfield

    class MyModel(models.Model):
        the_json = jsonfield.JSONField()

You can assign any JSON-encodable object to this field. It will be
JSON-encoded before being stored in the database as a text value and it
will be turned back into a python list/dict/string upon retrieval from the
database.

There is also a ``TypedJSONField``, that allows you to define data types that
must be included within each object in the array. More documentation to follow.

Notes
~~~~~

If no ``default`` is provided, and ``null=True`` is not passed in to the
field constructor, then a default of ``{}`` will be used.

Supported django versions
-------------------------

All versions of Django from 1.8 onwards are tested.

Extras
------

jsonify templatetag
~~~~~~~~~~~~~~~~~~~
This allows you to convert a python data structure into JSON within a template::

    {% load jsonify %}

    <script>
    var foo = {{ bar|jsonify|safe }};
    </script>

Note that you must only use the "safe" filter when you use the jsonify
filter within a <script> tag (which is parsed like a CDATA section).

If you use it in some other places like in an HTML attribute, then
you must not use the safe filter so that its output is properly escaped::

    <div data-foo="{{ bar|jsonify }}">

The above rules are important to avoid XSS attacks with unsafe strings
stored in the converted data structure.

Contributing
------------

If you want to contribute to django-jsonfield, it will help you to run
the test suite. This can be done in its most simple form by running::

  DB_ENGINE=sqlite3 DB_NAME=tests ./tests.py

To run the tests fully, you will need to install tox.


History
-------

1.4.0 (2019-12-18)
~~~~~~~~~~~~~~~~~~

* Django 3.0 compatbility. This required adding ``six`` as a dependency.

1.3.1 (2019-08-19)
~~~~~~~~~~~~~~~~~~

* Fix Python 2 compatilibity from change in 1.3.0
  (`PR #16 <https://github.com/adamchainz/django-jsonfield/pull/16>`__).

1.3.0 (2019-08-18)
~~~~~~~~~~~~~~~~~~

* Work in parallel with ``django.contrib.postgres.fields.JSONField`` by
  removing registration of default JSONB function and instead using Postgres'
  cast-to-text in SQL
  (`PR #14 <https://github.com/adamchainz/django-jsonfield/pull/14>`__).

  This should allow you to move to the better supported
  ``django.contrib.postgres.fields.JSONField``, and then Django 3.0's upcoming
  all-database ``JSONField``.

1.2.0 (2019-04-28)
~~~~~~~~~~~~~~~~~~

* Tested with Django 2.2.
* Stop "RemovedInDjango30Warning: Remove the context parameter from
  JSONField.from_db_value()." on Django 2.0+.

1.1.0 (2019-03-16)
~~~~~~~~~~~~~~~~~~

Django 1.10 support: register explicit lookup operators.

Django 1.11 support: update render() method for widget.

1.0.1 (2016-07-21)
~~~~~~~~~~~~~~~~~~

Fix issue with Postgres JSONB fields.

Limit XSS attacks with jsonify template tag.

1.0.0 (2016-06-02)
~~~~~~~~~~~~~~~~~~

Add support for Django 1.8 and 1.9 (without warnings). Remove support for Django < 1.8
as none of those releases are supported upstream anyway.

With this version, ``JSONField`` no longer decodes assigned string values as JSON. Instead it assumes that any value that you assign is the decoded value which will be JSON-encoded before storage in the database. This explains the bump to version 1.0 as it's a backwards incompatible change.

0.9.19 (2016-02-22)
~~~~~~~~~~~~~~~~~~~

Allow passing `decoder_kwargs` as an argument to a field. This dict will be passed as kwargs to
the `json.loads()` calls when loading data that is a string.

You may also set this as a global value in settings.JSONFIELD_DECODER_KWARGS.

A new dict is created for each field: so if this value is altered after field definition, it shouldn't
affect already attached fields.

0.9.16
~~~~~~
Allow passing an argument of `encoder_class` to a field, which will result in that object (or
the object located at that path, for instance `core.utils.JSONEncoder`) being used as the `cls`
argument when serializing objects.

You may also set this as a global value in settings.JSONFIELD_ENCODER_CLASS

0.9.15
~~~~~~
Bump version number to get around uploading issues.

0.9.14
~~~~~~
No longer hit the db to work out db_type.

0.9.12
~~~~~~
Cache the result of db_type.
Handle incoming data from multiple select widget better.

0.9.9
~~~~~
Finally strip out non-required files.

0.9.8
~~~~~
Remove freezegun workarounds.
Fix broken build.

0.9.4
~~~~~
Fixes for mutable defaults: we serialize and then deserialize in this
case, so you can still use ``default={}``.

0.9.3
~~~~~
Remove support for storing data using Postgres' 9.2's JSON data type, as
you cannot currently query against this!

Remove support for django < 1.3.


0.9.0
~~~~~
Add LICENSE file.
Added TypedJSONField.


0.8.10
~~~~~~
Allow ``{{ variable|jsonify }}`` to work with querysets.

0.8.8
~~~~~
Prevent circular import problem with django 1.3.1 and gargoyle.

0.8.7
~~~~~
Better handle null=True and blank=True: it should make sense what they do now.

0.8.5
~~~~~
Allow for '{}' and '[]', and make them not appear to be None.

0.8.4
~~~~~
Ensure the version number file is installed with the package.

0.8.3
~~~~~
Store the version number in one place only, now.

0.8.2
~~~~~
Oops. Packaging error prevented install from pypi. Added README.rst to manifest.

0.8.1
~~~~~
Converting to string does nothing, as serializing a model instance with a JSONField would have a string version of that field, instead of it embedded inline. (Back to pre 0.8 behaviour).

Added better querying support: (``field__contains={'key':'value','key2':'value2'}`` works.)

Removed JSONTableWidget from package.

0.8
~~~

(Many thanks to `IanLewis <https://bitbucket.org/IanLewis>`_ for these features)

Supports django 1.2

Supports callable and json serializable objects as default

Implemented get_db_prep_value()

Add tests and test runner.

Removed JSONTableWidget from README.

0.7.1
~~~~~

Don't fail when trying to install before django is installed.

0.7
~~~
First tagged release.