package documentation

Interfaces for serializing Django objects.

Usage:

from django.core import serializers
json = serializers.serialize("json", some_queryset)
objects = list(serializers.deserialize("json", json))

To add your own serializers, use the SERIALIZATION_MODULES setting:

SERIALIZATION_MODULES = {
    "csv": "path.to.csv.serializer",
    "txt": "path.to.txt.serializer",
}
Module json Serialize data to/from JSON
Module base Module for abstract serializer/unserializer base classes.
Module jsonl Serialize data to/from JSON Lines
Module python A Python "serializer". Doesn't do much serializing per se -- just converts to and from basic Python data types (lists, dicts, strings, etc.). Useful as a basis for other serializers.
Module pyyaml YAML serializer.
Module xml​_serializer XML serializer.

From __init__.py:

Function get​_serializer Undocumented
Constant BUILTIN​_SERIALIZERS Undocumented
Class ​Bad​Serializer Stub serializer to hold exception raised during registration
Function ​_load​_serializers Register built-in and settings-defined serializers. This is done lazily so that user code has a chance to (e.g.) set up custom settings without needing to be careful of import order.
Function deserialize No summary
Function get​_deserializer Undocumented
Function get​_public​_serializer​_formats Undocumented
Function get​_serializer​_formats Undocumented
Function register​_serializer Register a new serializer.
Function serialize Serialize a queryset (or any iterator that returns database objects) using a certain serializer.
Function sort​_dependencies Sort a list of (app_config, models) pairs into a single list of models.
Function unregister​_serializer Unregister a given serializer. This is not a thread-safe operation.
Variable ​_serializers Undocumented
BUILTIN_SERIALIZERS: dict[str, str] =

Undocumented

Value
{'xml': 'django.core.serializers.xml_serializer',
 'python': 'django.core.serializers.python',
 'json': 'django.core.serializers.json',
 'yaml': 'django.core.serializers.pyyaml',
 'jsonl': 'django.core.serializers.jsonl'}
_serializers: dict =

Undocumented

def register_serializer(format, serializer_module, serializers=None):

Register a new serializer.

serializer_module should be the fully qualified module name for the serializer.

If serializers is provided, the registration will be added to the provided dictionary.

If serializers is not provided, the registration will be made directly into the global register of serializers. Adding serializers directly is not a thread-safe operation.

def unregister_serializer(format):
Unregister a given serializer. This is not a thread-safe operation.
def get_serializer(format):

Undocumented

def get_serializer_formats():

Undocumented

def get_public_serializer_formats():

Undocumented

def get_deserializer(format):

Undocumented

def serialize(format, queryset, **options):
Serialize a queryset (or any iterator that returns database objects) using a certain serializer.
def deserialize(format, stream_or_string, **options):
Deserialize a stream or a string. Return an iterator that yields (obj, m2m_relation_dict), where obj is an instantiated -- but unsaved -- object, and m2m_relation_dict is a dictionary of {m2m_field_name : list_of_related_objects}.
def _load_serializers():
Register built-in and settings-defined serializers. This is done lazily so that user code has a chance to (e.g.) set up custom settings without needing to be careful of import order.
def sort_dependencies(app_list, allow_cycles=False):

Sort a list of (app_config, models) pairs into a single list of models.

The single list of models is sorted so that any model with a natural key is serialized before a normal model, and any model with a natural key dependency has it's dependencies serialized first.

If allow_cycles is True, return the best-effort ordering that will respect most of dependencies but ignore some of them to break the cycles.