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
|
|
|
|
|
|
|
|
# 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
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def register_serializer(format, serializer_module):
|
|
|
|
"""Register a new serializer by passing in a module name."""
|
2006-10-31 04:50:27 +08:00
|
|
|
module = __import__(serializer_module, {}, {}, [''])
|
2006-06-29 00:00:37 +08:00
|
|
|
_serializers[format] = module
|
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 unregister_serializer(format):
|
|
|
|
"""Unregister a given serializer"""
|
|
|
|
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
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
for format in BUILTIN_SERIALIZERS:
|
|
|
|
register_serializer(format, BUILTIN_SERIALIZERS[format])
|
|
|
|
if hasattr(settings, "SERIALIZATION_MODULES"):
|
|
|
|
for format in settings.SERIALIZATION_MODULES:
|
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
|
|
|
register_serializer(format, settings.SERIALIZATION_MODULES[format])
|