2006-06-29 00:00:37 +08:00
|
|
|
"""
|
|
|
|
Interfaces for serializing Django objects.
|
|
|
|
|
|
|
|
Usage::
|
|
|
|
|
2007-09-15 16:29:56 +08:00
|
|
|
from django.core import serializers
|
|
|
|
json = serializers.serialize("json", some_query_set)
|
|
|
|
objects = list(serializers.deserialize("json", json))
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
To add your own serializers, use the SERIALIZATION_MODULES setting::
|
|
|
|
|
|
|
|
SERIALIZATION_MODULES = {
|
|
|
|
"csv" : "path.to.csv.serializer",
|
|
|
|
"txt" : "path.to.txt.serializer",
|
|
|
|
}
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.conf import settings
|
2009-03-19 00:55:59 +08:00
|
|
|
from django.utils import importlib
|
2006-06-29 00:00:37 +08:00
|
|
|
|
|
|
|
# Built-in serializers
|
|
|
|
BUILTIN_SERIALIZERS = {
|
2006-06-30 00:42:49 +08:00
|
|
|
"xml" : "django.core.serializers.xml_serializer",
|
|
|
|
"python" : "django.core.serializers.python",
|
|
|
|
"json" : "django.core.serializers.json",
|
2006-06-29 00:00:37 +08:00
|
|
|
}
|
|
|
|
|
2007-03-12 22:01:44 +08:00
|
|
|
# Check for PyYaml and register the serializer if it's available.
|
|
|
|
try:
|
|
|
|
import yaml
|
|
|
|
BUILTIN_SERIALIZERS["yaml"] = "django.core.serializers.pyyaml"
|
|
|
|
except ImportError:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
pass
|
2007-03-12 22:01:44 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
_serializers = {}
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2008-08-15 19:53:49 +08:00
|
|
|
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.
|
|
|
|
"""
|
2009-03-19 00:55:59 +08:00
|
|
|
module = importlib.import_module(serializer_module)
|
2008-08-15 19:53:49 +08:00
|
|
|
if serializers is None:
|
|
|
|
_serializers[format] = module
|
|
|
|
else:
|
|
|
|
serializers[format] = module
|
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def unregister_serializer(format):
|
2008-08-15 19:53:49 +08:00
|
|
|
"Unregister a given serializer. This is not a thread-safe operation."
|
2006-06-29 00:00:37 +08:00
|
|
|
del _serializers[format]
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def get_serializer(format):
|
|
|
|
if not _serializers:
|
|
|
|
_load_serializers()
|
|
|
|
return _serializers[format].Serializer
|
2007-03-01 21:11:08 +08:00
|
|
|
|
|
|
|
def get_serializer_formats():
|
|
|
|
if not _serializers:
|
|
|
|
_load_serializers()
|
|
|
|
return _serializers.keys()
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2007-12-17 14:53:15 +08:00
|
|
|
def get_public_serializer_formats():
|
|
|
|
if not _serializers:
|
|
|
|
_load_serializers()
|
|
|
|
return [k for k, v in _serializers.iteritems() if not v.Serializer.internal_use_only]
|
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def get_deserializer(format):
|
|
|
|
if not _serializers:
|
|
|
|
_load_serializers()
|
|
|
|
return _serializers[format].Deserializer
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def serialize(format, queryset, **options):
|
|
|
|
"""
|
|
|
|
Serialize a queryset (or any iterator that returns database objects) using
|
|
|
|
a certain serializer.
|
|
|
|
"""
|
|
|
|
s = get_serializer(format)()
|
|
|
|
s.serialize(queryset, **options)
|
|
|
|
return s.getvalue()
|
|
|
|
|
|
|
|
def deserialize(format, stream_or_string):
|
|
|
|
"""
|
|
|
|
Deserialize a stream or a string. Returns an iterator that yields ``(obj,
|
|
|
|
m2m_relation_dict)``, where ``obj`` is a instantiated -- but *unsaved* --
|
|
|
|
object, and ``m2m_relation_dict`` is a dictionary of ``{m2m_field_name :
|
|
|
|
list_of_related_objects}``.
|
|
|
|
"""
|
|
|
|
d = get_deserializer(format)
|
|
|
|
return d(stream_or_string)
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2008-08-15 19:53:49 +08:00
|
|
|
global _serializers
|
|
|
|
serializers = {}
|
2006-06-29 00:00:37 +08:00
|
|
|
for format in BUILTIN_SERIALIZERS:
|
2008-08-15 19:53:49 +08:00
|
|
|
register_serializer(format, BUILTIN_SERIALIZERS[format], serializers)
|
2006-06-29 00:00:37 +08:00
|
|
|
if hasattr(settings, "SERIALIZATION_MODULES"):
|
|
|
|
for format in settings.SERIALIZATION_MODULES:
|
2008-08-15 19:53:49 +08:00
|
|
|
register_serializer(format, settings.SERIALIZATION_MODULES[format], serializers)
|
|
|
|
_serializers = serializers
|