kbussell / django-jsonfield-compat

Compatibility layer for django-jsonfield and Django's native JSONField

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

django-jsonfield-compat

Travis Build Status PyPI version Coverage Status

Django 1.9 introduced first-class support for Postgresql's json type.

Not everyone uses Postgresql, nor has upgraded to 1.9 yet, so a mixed ecosystem exists. Unfortunately, these two field types cannot be used together in the same project.

django-jsonfield-compat was created to be able to allow 3rd-party libraries to work in both worlds.

This project is safe to use on Django 1.8 and up. If your combination of Django version and backend database doesn't support the native JSONField implementation, you'll continue to use the underlying django-jsonfield implmentation.

Installation

pip install django-jsonfield-compat

Adjust all instances of importing JSONField and JSONFormField to jsonfield_compat's definition (including all migration files!)

from jsonfield_compat.fields import JSONField
from jsonfield_compat.forms import JSONFormField

Register every app that has a model containing a JSONField from the AppConfig class's ready() function.

Example apps.py:

from __future__ import unicode_literals

from django.apps import AppConfig


class MyAppConfig(AppConfig):
    name = 'myapp'
    verbose_name = "My App"

    def ready(self):
        import jsonfield_compat
        jsonfield_compat.register_app(self)

Now, when users of your application want to switch from using django-jsonfield's implementation to Django's native JSONField, they add the setting:

USE_NATIVE_JSONFIELD = True

And then run python manage.py migrate to convert the app's DB tables to use the JSON column type.

To revert back to using non-native fields, change the setting to False and run migrate again.

License

django-jsonfield-compat is licensed under the MIT license (see the LICENSE file for details).

About

Compatibility layer for django-jsonfield and Django's native JSONField

License:MIT License


Languages

Language:Python 100.0%