Menu
Introduction
Getting Started
Developer guide
Release
Architecture
CLI
Technical documentations
Version
Publication date

Mar 9, 2022

Confidentiality
Public
Reactions
1
Share

DictRField

Resource field for storing JSON-like dictionaries.

DictRField stores dictionary data with automatic JSON serialization and validation. It only supports JSON-compatible values: primitives (str, int, float, bool, None), nested dictionaries, and lists of these types.

The field automatically: - Converts Python objects to JSON-compatible format using str() - Replaces NaN and Inf values with None - Validates that values are dictionaries - Provides an empty dict {} as default if no default_value is specified

This is useful for: - Storing metadata and configuration - Nested structured data - API responses and JSON data - Key-value mappings

Storage behavior: - Stored in KV_STORE by default (inherited from PrimitiveRField) - Not included in dict views by default (can be changed) - Automatically serialized to JSON on save - Automatically deserialized from JSON on load

Example: ```python class MyResource(Resource): # Simple metadata dictionary metadata = DictRField()

    # With default value
    config = DictRField(default_value={'key': 'value'})

    # Included in dict view
    summary = DictRField(include_in_dict_view=True)

# Usage
resource = MyResource()
resource.metadata = {'author': 'John', 'version': 1}
resource.config['new_key'] = 'new_value'
```

Note: - Non-JSON-compatible objects are converted to strings - NaN and Inf are replaced with None for JSON compatibility - Nested structures must also be JSON-compatible

Attributes
include_in_dict_view: boolstorage: RFieldStoragevalidator: Validator
Functions
__init__

Initialize a DictRField for storing JSON-like dictionaries.

default_value : dict | None
Default value for the field when not set. Can be: - Dict: A dictionary with JSON-compatible values - Type or Callable: Called without parameters to generate default (e.g., dict) - None: Defaults to empty dict {} The default value must be JSON-serializable. Defaults to None (uses {})
include_in_dict_view : bool - False
If True, this field is included in dict/JSON representations. Set to False for large dictionaries to avoid performance issues. Defaults to False
deserialize

Deserialize and validate a value from storage.

Validates the stored value using the field's validator before returning it.

r_field_value : Any
The raw value from storage
Return type : Any
get_default_value

Get the default value for this field.

Returns an empty dictionary if no default value was specified. If a default value was provided, it is converted to JSON format to ensure compatibility.

Return type : Any
serialize

Serialize the dictionary value for storage.

Converts the dictionary to JSON-compatible format by: - Converting non-JSON objects to strings - Replacing NaN and Inf with None - Ensuring nested structures are JSON-compatible

r_field_value : Any
The dictionary value to serialize
Return type : Any
Shine Logo
Technical bricks to reuse or customize

Have you developed a brick?

Share it to accelerate projects for the entire community.