djblets.db.fields.json_field¶
-
class
JSONFormField
(encoder=None, encoder_cls=None, encoder_kwargs=None, *args, **kwargs)[source]¶ Bases:
django.forms.fields.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
andencoder_kwargs
, and will prevent the field from nicely formatting the contents for editing.Deprecated since version 1.0.1:
encoder_cls
andencoder_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.
- encoder (json.JSONEncoder, optional) –
-
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=None, encoder_cls=None, encoder_kwargs=None, **kwargs)[source]¶ Bases:
django.db.models.fields.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.-
__init__
(verbose_name=None, name=None, encoder=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 (json.JSONEncoder, optional) –
The explicit JSON encoder instance to use. If specified, this takes priority over
encoder_cls
andencoder_kwargs
, and will prevent the field from nicely formatting the contents for editing.Deprecated since version 1.0.1:
encoder_cls
andencoder_kwargs
should be used instead. Specifyingencoder
will emit a warning. - 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.
-
contribute_to_class
(cls, name)[source]¶ Add methods/signal handlers to the model owning this field.
This will add
get_fieldname_json()
andset_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:
-
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_db_prep_save
(value, *args, **kwargs)[source]¶ Return the serialized value prepared for saving to the database.
Parameters: Returns: The resulting serialized data that can be saved to the database.
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
-
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.
This is only used on Django 1.7+.
New in version 0.9.
Returns: A tuple of (name, module path, positional arguments, keyword arguments). Return type: tuple
-