• Get Review Board
  • What's New
  • Products
  • Review Board Code review, image review, and document review
  • Documentation
  • Release Notes
  • Power Pack Enterprise integrations, reports, and enhanced document review
  • Try for 60 Days
  • Purchase
  • RBCommons Review Board as a Service, hosted by us
  • Pricing
  • RBTools Command line tools and Python API for Review Board
  • Documentation
  • Release Notes
  • Review Bot Automated code review, connecting tools you already use
  • Documentation
  • Release Notes
  • RB Gateway Manage Git and Mercurial repositories in your network
  • Documentation
  • Release Notes
  • Learn and Explore
  • What is Code Review?
  • Documentation
  • Frequently Asked Questions
  • Support Options
  • Third-Party Integrations
  • Demo
  • Review Board RBTools Power Pack Review Bot Djblets RB Gateway
    1. Djblets 4.x
    2. Version 5.x
    3. Version 4.x
    4. Version 3.x
    5. Version 2.x
    6. Version 2.0
    7. Version 1.0
    8. Version 0.9
    9. Djblets Documentation
    10. Module and Class References
    11. djblets.db.fields.json_field
  • Home
  • Guides
  • Avatar Services Guides
  • Writing Avatar Services
  • Extension Guides
  • Writing Extensions
  • Testing Extensions
  • Feature Checks Guides
  • Introduction to Feature Checks
  • Writing Features
  • Writing Feature Checkers
  • Testing with Feature Checks
  • Integration Guides
  • Supporting Integrations
  • Writing Integrations
  • Privacy Compliance Guides
  • Getting and Checking Consent
  • Working with Personally Identifiable Information
  • Service Integrations
  • reCAPTCHA Guides
  • Using reCAPTCHA
  • Registries Guides
  • Writing Registries
  • Web API Guides
  • Writing Web API Resources
  • Adding OAuth2 Support
  • Module and Class References
  • djblets
  • djblets.deprecation
  • djblets.auth.forms
  • djblets.auth.ratelimit
  • djblets.auth.signals
  • djblets.auth.util
  • djblets.auth.views
  • djblets.avatars.errors
  • djblets.avatars.forms
  • djblets.avatars.registry
  • djblets.avatars.services
  • djblets.avatars.services.base
  • djblets.avatars.services.fallback
  • djblets.avatars.services.file_upload
  • djblets.avatars.services.gravatar
  • djblets.avatars.services.url
  • djblets.avatars.settings
  • djblets.cache.backend
  • djblets.cache.backend_compat
  • djblets.cache.context_processors
  • djblets.cache.errors
  • djblets.cache.forwarding_backend
  • djblets.cache.serials
  • djblets.cache.synchronizer
  • djblets.conditions
  • djblets.conditions.choices
  • djblets.conditions.conditions
  • djblets.conditions.errors
  • djblets.conditions.operators
  • djblets.conditions.values
  • djblets.configforms.forms
  • djblets.configforms.mixins
  • djblets.configforms.pages
  • djblets.configforms.registry
  • djblets.configforms.views
  • djblets.datagrid.grids
  • djblets.db.backends.mysql.base
  • djblets.db.fields
  • djblets.db.fields.base64_field
  • djblets.db.fields.counter_field
  • djblets.db.fields.json_field
  • djblets.db.fields.modification_timestamp_field
  • djblets.db.fields.relation_counter_field
  • djblets.db.managers
  • djblets.db.query
  • djblets.db.validators
  • djblets.extensions.admin
  • djblets.extensions.errors
  • djblets.extensions.extension
  • djblets.extensions.forms
  • djblets.extensions.hooks
  • djblets.extensions.loaders
  • djblets.extensions.manager
  • djblets.extensions.middleware
  • djblets.extensions.models
  • djblets.extensions.packaging
  • djblets.extensions.resources
  • djblets.extensions.settings
  • djblets.extensions.signals
  • djblets.extensions.staticfiles
  • djblets.extensions.testing
  • djblets.extensions.testing.testcases
  • djblets.extensions.urls
  • djblets.extensions.views
  • djblets.extensions.templatetags.djblets_extensions
  • djblets.features
  • djblets.features.checkers
  • djblets.features.decorators
  • djblets.features.errors
  • djblets.features.feature
  • djblets.features.level
  • djblets.features.registry
  • djblets.features.testing
  • djblets.features.templatetags.features
  • djblets.feedview.views
  • djblets.feedview.templatetags.feedtags
  • djblets.forms.fields
  • djblets.forms.fieldsets
  • djblets.forms.forms
  • djblets.forms.forms.key_value_form
  • djblets.forms.widgets
  • djblets.gravatars
  • djblets.gravatars.templatetags.gravatars
  • djblets.http.middleware
  • djblets.http.responses
  • djblets.integrations.errors
  • djblets.integrations.forms
  • djblets.integrations.hooks
  • djblets.integrations.integration
  • djblets.integrations.manager
  • djblets.integrations.mixins
  • djblets.integrations.models
  • djblets.integrations.urls
  • djblets.integrations.views
  • djblets.log
  • djblets.log.middleware
  • djblets.log.siteconfig
  • djblets.log.urls
  • djblets.log.views
  • djblets.mail.dmarc
  • djblets.mail.message
  • djblets.mail.testing
  • djblets.mail.utils
  • djblets.markdown
  • djblets.markdown.extensions.escape_html
  • djblets.markdown.extensions.wysiwyg
  • djblets.markdown.extensions.wysiwyg_email
  • djblets.pipeline.compilers.es6
  • djblets.pipeline.compilers.less
  • djblets.pipeline.compilers.mixins
  • djblets.pipeline.compilers.rollup
  • djblets.pipeline.compilers.typescript
  • djblets.pipeline.settings
  • djblets.privacy.consent
  • djblets.privacy.consent.base
  • djblets.privacy.consent.common
  • djblets.privacy.consent.errors
  • djblets.privacy.consent.forms
  • djblets.privacy.consent.hooks
  • djblets.privacy.consent.registry
  • djblets.privacy.consent.tracker
  • djblets.privacy.models
  • djblets.privacy.pii
  • djblets.privacy.templatetags.djblets_privacy
  • djblets.recaptcha.mixins
  • djblets.recaptcha.siteconfig
  • djblets.recaptcha.templatetags.djblets_recaptcha
  • djblets.recaptcha.widgets
  • djblets.registries
  • djblets.registries.errors
  • djblets.registries.importer
  • djblets.registries.mixins
  • djblets.registries.registry
  • djblets.registries.signals
  • djblets.secrets
  • djblets.secrets.crypto
  • djblets.secrets.token_generators
  • djblets.secrets.token_generators.base
  • djblets.secrets.token_generators.legacy_sha1
  • djblets.secrets.token_generators.registry
  • djblets.secrets.token_generators.vendor_checksum
  • djblets.siteconfig
  • djblets.siteconfig.admin
  • djblets.siteconfig.context_processors
  • djblets.siteconfig.django_settings
  • djblets.siteconfig.forms
  • djblets.siteconfig.managers
  • djblets.siteconfig.middleware
  • djblets.siteconfig.models
  • djblets.siteconfig.signals
  • djblets.siteconfig.views
  • djblets.template.caches
  • djblets.template.context
  • djblets.template.loaders.conditional_cached
  • djblets.template.loaders.namespaced_app_dirs
  • djblets.testing.decorators
  • djblets.testing.testcases
  • djblets.testing.testrunners
  • djblets.urls.context_processors
  • djblets.urls.decorators
  • djblets.urls.patterns
  • djblets.urls.resolvers
  • djblets.urls.root
  • djblets.urls.staticfiles
  • djblets.util.compat.django.core.cache
  • djblets.util.compat.python.past
  • djblets.util.contextmanagers
  • djblets.util.dates
  • djblets.util.decorators
  • djblets.util.filesystem
  • djblets.util.functional
  • djblets.util.html
  • djblets.util.http
  • djblets.util.humanize
  • djblets.util.json_utils
  • djblets.util.properties
  • djblets.util.serializers
  • djblets.util.symbols
  • djblets.util.templatetags.djblets_deco
  • djblets.util.templatetags.djblets_email
  • djblets.util.templatetags.djblets_forms
  • djblets.util.templatetags.djblets_images
  • djblets.util.templatetags.djblets_js
  • djblets.util.templatetags.djblets_utils
  • djblets.util.typing
  • djblets.util.views
  • djblets.views.generic.base
  • djblets.views.generic.etag
  • djblets.webapi.auth
  • djblets.webapi.auth.backends
  • djblets.webapi.auth.backends.api_tokens
  • djblets.webapi.auth.backends.base
  • djblets.webapi.auth.backends.basic
  • djblets.webapi.auth.backends.oauth2_tokens
  • djblets.webapi.auth.views
  • djblets.webapi.decorators
  • djblets.webapi.encoders
  • djblets.webapi.errors
  • djblets.webapi.fields
  • djblets.webapi.managers
  • djblets.webapi.models
  • djblets.webapi.oauth2_scopes
  • djblets.webapi.resources
  • djblets.webapi.resources.base
  • djblets.webapi.resources.group
  • djblets.webapi.resources.registry
  • djblets.webapi.resources.root
  • djblets.webapi.resources.user
  • djblets.webapi.resources.mixins.api_tokens
  • djblets.webapi.resources.mixins.forms
  • djblets.webapi.resources.mixins.oauth2_tokens
  • djblets.webapi.resources.mixins.queries
  • djblets.webapi.responses
  • djblets.webapi.signals
  • djblets.webapi.testing
  • djblets.webapi.testing.decorators
  • djblets.webapi.testing.testcases
  • General Index
  • Python Module Index
  • Release Notes
  • This documentation covers Djblets 4.x. You can select a version above or view the latest documentation.

    djblets.db.fields.json_field¶

    JSONValue¶

    A type indicating a valid value in JSON data.

    As of the time this was added, Python does not have its own JSON value definition, and type checkers do not allow for recursive definitions. This is currently very generic, but may be expanded or aliased in the future.

    Deprecated since version 3.3: Consumers should use the more complete djblets.util.typing.JSONValue instead.

    New in version 3.1.

    JSONDict¶

    A type for a JSON dictionary.

    Deprecated since version 3.3: Consumers should use the more complete djblets.util.typing.JSONDict instead.

    New in version 3.1.

    alias of Dict[str, object]

    class JSONFormField(encoder=None, encoder_cls=None, encoder_kwargs=None, *args, **kwargs)[source]¶

    Bases: CharField

    Provides a form field for JSON input.

    This is meant to be used by JSONField, and handles the work of rendering JSON content in a more human-editable way for editing (by sorting keys and adding indentation), and then validating and returning the resulting JSON document back for storage in the field.

    encoder¶

    The JSON encoder being used to serialize JSON data for editing.

    Type:

    json.JSONEncoder

    __init__(encoder=None, encoder_cls=None, encoder_kwargs=None, *args, **kwargs)[source]¶

    Initialize the field.

    Parameters:
    • encoder (json.JSONEncoder, optional) –

      The explicit JSON encoder instance to use. If specified, this takes priority over encoder_cls and encoder_kwargs, and will prevent the field from nicely formatting the contents for editing.

      Deprecated since version 1.0.1: encoder_cls and encoder_kwargs should be used instead.

    • encoder_cls (type, optional) – The type of encoder to use for serializing the JSON document for editing. This must be a subclass of json.JSONEncoder.

    • encoder_kwargs (dict, optional) – Keyword arguments to pass to the constructor for the encoder.

    • *args (tuple) – Extra positional arguments to pass to the field.

    • **kwargs (dict) – Extra keyword arguments to pass to the field.

    prepare_value(value)[source]¶

    Prepare a field’s value for storage in the database.

    This will encode the value to JSON (unless it’s already a string).

    Parameters:

    value (object) – The JSON-serializable value to encode.

    Returns:

    The resulting JSON string.

    Return type:

    unicode

    to_python(value)[source]¶

    Return the Python representation of the value in the field.

    This will attempt to deserialize the value and return it. If the value is not a string, it will be returned directly.

    Parameters:

    value (object) – The value stored for the field. This is expected to be a string representing serialized JSON data.

    Returns:

    The deserialized JSON object, or None if it’s an empty string.

    Return type:

    object

    Raises:

    django.core.exceptions.ValidationError – The value was not able to be deserialized as JSON content.

    class JSONField(verbose_name=None, name=None, encoder_cls=None, encoder_kwargs=None, **kwargs)[source]¶

    Bases: TextField

    A field for storing JSON-encoded data.

    The data is accessible as standard Python data types and is transparently encoded to and decoded from a JSON string in the database.

    Consumers can specify custom encoding behavior by providing an encoder class and keyword arguments. By default, DjbletsJSONEncoder is used, which supports lazy strings, datetimes, and model-specified custom encoding behavior.

    serialize_to_string = True[source]¶
    default_validators = [<function validate_json>][source]¶
    default_error_messages = {'invalid_type': '%(type)s is not a supported value type.'}[source]¶
    __init__(verbose_name=None, name=None, encoder_cls=None, encoder_kwargs=None, **kwargs)[source]¶

    Initialize the field.

    Parameters:
    • verbose_name (unicode, optional) – The verbose name shown for the field.

    • name (unicode, optional) – The attribute name of the field.

    • encoder_cls (type, optional) – The type of encoder to use for serializing the JSON document for storage and editing. This must be a subclass of json.JSONEncoder.

    • encoder_kwargs (dict, optional) – Keyword arguments to pass to the constructor for the encoder. This may be modified by the field.

    • **kwargs (dict) – Additional keyword arguments for the field.

    encoder[source]¶

    An encoder instance used for this field.

    contribute_to_class(cls, name)[source]¶

    Add methods/signal handlers to the model owning this field.

    This will add get_fieldname_json() and set_fieldname_json() methods to the model, which will allow retrieving and setting the serialized data directly.

    It also listens for when an instance of the model is initialized and sets the field to point to the deserialized JSON data.

    Parameters:
    • cls (type) – The Model class to contribute to.

    • name (unicode) – The name of this field on the model.

    pre_save(model_instance, add)[source]¶

    Return serialized field data to save on a model instance.

    Parameters:
    • model_instance (django.db.models.Model) – The model instance being saved.

    • add (bool, unused) – Whether this is a new instance being added to the database. This is ignored.

    Returns:

    The serialized data to save.

    Return type:

    unicode

    post_init(instance, **kwargs)[source]¶

    Handle initialization of a model instance.

    This will deserialize the stored data from the database and set it as the field data on the model.

    Parameters:
    • instance (django.db.models.Model) – The model instance being initialized.

    • **kwargs (dict) – Extra keyword arguments from the signal.

    get_prep_value(value)[source]¶

    Return the serialized value prepared for storage.

    This will serialize the data, if it’s a JSON structure and not already a string, preparing it for storage in the database or another location.

    Parameters:

    value (object) – The value to prepare.

    Returns:

    The serialized representation of the value.

    Return type:

    unicode

    value_to_string(obj)[source]¶

    Return the serialized JSON data from the field.

    Parameters:

    obj (django.db.models.Model) – The model instance containing the field.

    Returns:

    The serialized JSON data from the field.

    Return type:

    unicode

    to_python(value)[source]¶

    Return a value suitable for using in Python code.

    This will return the deserialized version of the data, allowing Python code to work with the data directly.

    Parameters:

    value (object) – The value to make usable in Python code.

    Returns:

    The deserialized data.

    Return type:

    object

    dumps(data)[source]¶

    Return serialized version of the provided JSON document.

    If the data is already a string, it will be provided directly. Otherwise, it will use the field’s associated JSON encoder to serialize the data.

    Parameters:

    data (object) – The data to serialize.

    Returns:

    The serialized JSON data.

    Return type:

    unicode

    loads(val)[source]¶

    Return a JSON document from the serialized JSON data.

    This will first attempt to deserialize the JSON data using the standard JSON decoder. If it’s unable to do so, or it gets back what appears to be a double-encoded JSON document or a Python string representation of a JSON document, it will attempt to parse the value and return a proper representation.

    Parameters:

    val (unicode) – The serialized JSON data to deserialize.

    Returns:

    The deserialized JSON document.

    Return type:

    object

    formfield(**kwargs)[source]¶

    Return a form field that can be used to edit this data.

    Parameters:

    **kwargs (dict) – Keyword arguments to pass to the subclass.

    Returns:

    The resulting form field.

    Return type:

    JSONFormField

    deconstruct()[source]¶

    Deconstruct the field for Django migrations.

    This makes JSONField migration-safe by encoding the default value to a string so that it can be safely loaded into the database.

    New in version 0.9.

    Returns:

    A tuple of (name, module path, positional arguments, keyword arguments).

    Return type:

    tuple

    __annotations__ = {}¶

    Keep up with the latest Review Board releases, security updates, and helpful information.

    About
    News
    Demo
    RBCommons Hosting
    Integrations
    Happy Users
    Support Options
    Documentation
    FAQ
    User Manual
    RBTools
    Administration Guide
    Power Pack
    Release Notes
    Downloads
    Review Board
    RBTools
    Djblets
    Power Pack
    Package Store
    PGP Signatures
    Contributing
    Bug Tracker
    Submit Patches
    Development Setup
    Wiki
    Follow Us
    Mailing Lists
    Reddit
    Twitter
    Mastodon
    Facebook
    YouTube

    Copyright © 2006-2025 Beanbag, Inc. All rights reserved.

    Terms of Service — Privacy Policy — AI Ethics Policy — Branding

    On this page

    • [Top]
    • JSONValue
    • JSONDict
    • JSONFormField
      • JSONFormField.encoder
      • JSONFormField.__init__()
      • JSONFormField.prepare_value()
      • JSONFormField.to_python()
    • JSONField
      • JSONField.serialize_to_string
      • JSONField.default_validators
      • JSONField.default_error_messages
      • JSONField.__init__()
      • JSONField.encoder
      • JSONField.contribute_to_class()
      • JSONField.pre_save()
      • JSONField.post_init()
      • JSONField.get_prep_value()
      • JSONField.value_to_string()
      • JSONField.to_python()
      • JSONField.dumps()
      • JSONField.loads()
      • JSONField.formfield()
      • JSONField.deconstruct()
      • JSONField.__annotations__