Previous topic

reverse

Next topic

status

This Page

serializer

Customizable serialization.

class serializer.Serializer(depth=None, stack=[], **kwargs)[source]

Converts python objects into plain old native types suitable for serialization. In particular it handles models and querysets.

The output format is specified by setting a number of attributes on the class.

You may also override any of the serialization methods, to provide for more flexible behavior.

Valid output types include anything that may be directly rendered into json, xml etc...

fields = ()

Specify the fields to be serialized on a model or dict. Overrides include and exclude.

include = ()

Fields to add to the default set to be serialized on a model/dict.

exclude = ()

Fields to remove from the default set to be serialized on a model/dict.

rename = {}

A dict of key->name to use for the field keys.

related_serializer = None

The default serializer class to use for any related models.

depth = None

The maximum depth to serialize to, or None.

get_default_fields(obj)[source]

Return the default list of field names/keys for a model instance/dict. These are used if fields is not given.

serialize_key(key)[source]

Keys serialize to their string value, unless they exist in the rename dict.

serialize_val(key, obj, related_info)[source]

Convert a model field or dict value into a serializable representation.

serialize_max_depth(obj)[source]

Determine how objects should be serialized once depth is exceeded. The default behavior is to ignore the field.

serialize_recursion(obj)[source]

Determine how objects should be serialized if recursion occurs. The default behavior is to ignore the field.

serialize_model(instance)[source]

Given a model instance or dict, serialize it to a dict..

serialize_iter(obj)[source]

Convert iterables into a serializable representation.

serialize_func(obj)[source]

Convert no-arg methods and functions into a serializable representation.

serialize_manager(obj)[source]

Convert a model manager into a serializable representation.

serialize_fallback(obj)[source]

Convert any unhandled object into a serializable representation.

serialize(obj)[source]

Convert any object into a serializable representation.