A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/dmkoch/django-jsonfield below:

rpkilby/jsonfield: A reusable Django model field for storing ad-hoc JSON data

jsonfield is a reusable model field that allows you to store validated JSON, automatically handling serialization to and from the database. To use, add jsonfield.JSONField to one of your models.

Deprecation & Migration to Django's native JSONField

Django 3.1 introduced a native JSONField that supports all database backends. As such, this package is considered deprecated and will be archived in the future. Existing projects should migrate to Django's implemenation.

Migrating from jsonfield.JSONField to models.JSONField should generally be straightforward. After swapping field classes, python manage.py migrate will generate AlterField operations that should correctly migrate your field data. However, if this does not work for your case, you will instead need to create a data migration. The process will roughly look like:

Examples can be found in the migration-example project.

from django.db import models
from jsonfield import JSONField

class MyModel(models.Model):
    json = JSONField()

As stated above, JSONField is not intended to provide extended querying capabilities. That said, you may perform the same basic lookups provided by regular text fields (e.g., exact or regex lookups). Since values are stored as serialized JSON, it is highly recommended that you test your queries to ensure the expected results are returned.

A model field's null argument typically controls whether null values may be stored in its column by setting a not-null constraint. However, because JSONField serializes its values (including nulls), this option instead controls how null values are persisted. If null=True, then nulls are not serialized and are stored as a null value in the database. If null=False, then the null is instead stored in its serialized form.

This in turn affects how null values may be queried. Both fields support exact matching:

MyModel.objects.filter(json=None)

However, if you want to use the isnull lookup, you must set null=True.

class MyModel(models.Model):
    json = JSONField(null=True)

MyModel.objects.filter(json__isnull=True)

Note that as JSONField.null does not prevent nulls from being stored, achieving this must instead be handled with a validator.

By default python deserializes json into dict objects. This behavior differs from the standard json behavior because python dicts do not have ordered keys. To overcome this limitation and keep the sort order of OrderedDict keys the deserialisation can be adjusted on model initialisation:

import collections

class MyModel(models.Model):
    json = JSONField(load_kwargs={'object_pairs_hook': collections.OrderedDict})

jsonfield.JSONCharField

Subclasses models.CharField instead of models.TextField.

The test suite requires tox.

Then, run the tox command, which will run all test jobs.

Or, to test just one job (for example Django 5.2 on Python 3.13):

$ pip install -U pip setuptools wheel twine
$ rm -rf dist/ build/
$ python setup.py sdist bdist_wheel
$ twine upload -r test dist/*
$ twine upload dist/*

Take a look at the changelog.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4