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

Mar 9, 2022

Confidentiality
Public
Reactions
1
Share

ListRField

Resource field for storing JSON-like lists.

ListRField stores list data with automatic JSON serialization and validation. It only supports JSON-compatible values: primitives (str, int, float, bool, None), nested dictionaries, and nested 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 lists - Provides an empty list [] as default if no default_value is specified

This is useful for: - Storing collections of items - Arrays of primitives or objects - Ordered data sequences - API responses with list data

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 list of items items = ListRField()

    # With default value
    tags = ListRField(default_value=['default', 'tags'])

    # Included in dict view
    categories = ListRField(include_in_dict_view=True)

# Usage
resource = MyResource()
resource.items = [1, 2, 3, 'text']
resource.tags.append('new_tag')
resource.categories = [{'name': 'cat1', 'id': 1}, {'name': 'cat2', 'id': 2}]
```

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 - Lists can contain mixed types as long as they're JSON-compatible

Attributes
include_in_dict_view: boolstorage: RFieldStoragevalidator: Validator
Functions
__init__

Initialize a ListRField for storing JSON-like lists.

default_value : list | None
Default value for the field when not set. Can be: - List: A list with JSON-compatible values - Type or Callable: Called without parameters to generate default (e.g., list) - None: Defaults to empty list [] 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 lists 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 list 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 list value for storage.

Converts the list 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 list 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.