SerializableRField
Resource field for storing custom objects with manual JSON serialization.
SerializableRField stores instances of SerializableObjectJson subclasses, which provide custom serialize() and deserialize() methods for converting between Python objects and JSON-compatible representations.
When a Resource is saved: - The object's serialize() method is called - The resulting JSON data is stored in KV_STORE
When a Resource is loaded: - The JSON data is retrieved - The object's deserialize() class method is called to reconstruct the object
This is useful for: - Custom objects with specific serialization requirements - Legacy objects that need special handling - Objects that can't use Pydantic (use ModelRField for Pydantic models) - Fine-grained control over serialization format
Storage behavior: - Stored in KV_STORE by default - Not included in dict views by default (can be changed) - Uses custom serialize/deserialize methods
Example: ```python class CustomData(SerializableObjectJson): def init(self, value: int = 0): self.value = value
def serialize(self) -> Dict:
return {'value': self.value}
@classmethod
def deserialize(cls, data: Dict) -> 'CustomData':
return cls(value=data['value'])
class MyResource(Resource):
# Default value is object_type() (new instance)
data = SerializableRField(object_type=CustomData)
# Included in dict view
visible_data = SerializableRField(
object_type=CustomData,
include_in_dict_view=True
)
# Usage
resource = MyResource()
resource.data = CustomData(value=42)
```
Note: - The object_type must be a subclass of SerializableObjectJson - Default value is a new instance of object_type (calls object_type()) - The object_type must have a no-argument constructor
include_in_dict_view: boolobject_type: typestorage: RFieldStorageInitialize a SerializableRField for storing custom serializable objects.
typebool - FalseDeserialize JSON data back to a SerializableObjectJson instance.
This method is called when loading the field from storage. It delegates to the object's deserialize() class method to reconstruct the object.
AnySerializableObjectJsonGet the default value for this field.
If the default value is a Type or Callable, it will be called to generate a new default value. Otherwise, the default value is returned directly.
This ensures that mutable defaults (like lists or dicts) are not shared between Resource instances.
AnySerialize a SerializableObjectJson instance to JSON-compatible data.
This method is called when saving the field to storage. It delegates to the object's serialize() method to convert it to JSON-compatible format.
SerializableObjectJsonAny