2006-06-30 00:42:49 +08:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
2006-06-30 00:42:49 +08:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.serializers import base
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.db import models, DEFAULT_DB_ALIAS
|
2012-07-21 16:00:10 +08:00
|
|
|
from django.utils.encoding import smart_text, is_protected_type
|
2012-07-21 03:14:27 +08:00
|
|
|
from django.utils import six
|
2006-06-30 00:42:49 +08:00
|
|
|
|
2012-09-30 20:34:13 +08:00
|
|
|
|
2006-06-30 00:42:49 +08:00
|
|
|
class Serializer(base.Serializer):
|
|
|
|
"""
|
|
|
|
Serializes a QuerySet to basic Python objects.
|
|
|
|
"""
|
2009-04-13 20:35:49 +08:00
|
|
|
|
2007-12-17 14:53:15 +08:00
|
|
|
internal_use_only = True
|
2009-04-13 20:35:49 +08:00
|
|
|
|
2006-06-30 00:42:49 +08:00
|
|
|
def start_serialization(self):
|
|
|
|
self._current = None
|
|
|
|
self.objects = []
|
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-30 00:42:49 +08:00
|
|
|
def end_serialization(self):
|
|
|
|
pass
|
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-30 00:42:49 +08:00
|
|
|
def start_object(self, obj):
|
|
|
|
self._current = {}
|
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-30 00:42:49 +08:00
|
|
|
def end_object(self, obj):
|
2012-05-26 17:43:37 +08:00
|
|
|
self.objects.append(self.get_dump_object(obj))
|
2006-06-30 00:42:49 +08:00
|
|
|
self._current = None
|
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
|
|
|
|
2012-05-26 17:43:37 +08:00
|
|
|
def get_dump_object(self, obj):
|
|
|
|
return {
|
2012-07-21 16:00:10 +08:00
|
|
|
"pk": smart_text(obj._get_pk_val(), strings_only=True),
|
|
|
|
"model": smart_text(obj._meta),
|
2012-05-26 17:43:37 +08:00
|
|
|
"fields": self._current
|
|
|
|
}
|
|
|
|
|
2006-06-30 00:42:49 +08:00
|
|
|
def handle_field(self, obj, field):
|
2009-04-13 20:35:49 +08:00
|
|
|
value = field._get_val_from_obj(obj)
|
|
|
|
# Protected types (i.e., primitives like None, numbers, dates,
|
|
|
|
# and Decimals) are passed through as is. All other values are
|
|
|
|
# converted to string first.
|
|
|
|
if is_protected_type(value):
|
|
|
|
self._current[field.name] = value
|
|
|
|
else:
|
|
|
|
self._current[field.name] = field.value_to_string(obj)
|
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-30 00:42:49 +08:00
|
|
|
def handle_fk_field(self, obj, field):
|
2012-02-05 02:27:07 +08:00
|
|
|
if self.use_natural_keys and hasattr(field.rel.to, 'natural_key'):
|
|
|
|
related = getattr(obj, field.name)
|
2012-03-13 03:41:31 +08:00
|
|
|
if related:
|
|
|
|
value = related.natural_key()
|
|
|
|
else:
|
|
|
|
value = None
|
2012-02-05 02:27:07 +08:00
|
|
|
else:
|
|
|
|
value = getattr(obj, field.get_attname())
|
|
|
|
self._current[field.name] = value
|
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-30 00:42:49 +08:00
|
|
|
def handle_m2m_field(self, obj, field):
|
2009-11-03 22:02:49 +08:00
|
|
|
if field.rel.through._meta.auto_created:
|
2009-12-14 20:39:20 +08:00
|
|
|
if self.use_natural_keys and hasattr(field.rel.to, 'natural_key'):
|
|
|
|
m2m_value = lambda value: value.natural_key()
|
|
|
|
else:
|
2012-07-21 16:00:10 +08:00
|
|
|
m2m_value = lambda value: smart_text(value._get_pk_val(), strings_only=True)
|
2009-12-14 20:39:20 +08:00
|
|
|
self._current[field.name] = [m2m_value(related)
|
2008-08-12 20:58:33 +08:00
|
|
|
for related in getattr(obj, field.name).iterator()]
|
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-30 00:42:49 +08:00
|
|
|
def getvalue(self):
|
|
|
|
return self.objects
|
|
|
|
|
2012-09-30 20:34:13 +08:00
|
|
|
|
2006-06-30 00:42:49 +08:00
|
|
|
def Deserializer(object_list, **options):
|
|
|
|
"""
|
|
|
|
Deserialize simple Python objects back into Django ORM instances.
|
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-30 00:42:49 +08:00
|
|
|
It's expected that you pass the Python objects themselves (instead of a
|
|
|
|
stream or a string) to the constructor
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
db = options.pop('using', DEFAULT_DB_ALIAS)
|
2012-09-30 20:34:13 +08:00
|
|
|
ignore = options.pop('ignorenonexistent', False)
|
|
|
|
|
2006-06-30 00:42:49 +08:00
|
|
|
models.get_apps()
|
|
|
|
for d in object_list:
|
|
|
|
# Look up the model and starting build a dict of data for it.
|
|
|
|
Model = _get_model(d["model"])
|
2013-02-12 14:00:38 +08:00
|
|
|
data = {Model._meta.pk.attname: Model._meta.pk.to_python(d.get("pk", None))}
|
2006-06-30 00:42:49 +08:00
|
|
|
m2m_data = {}
|
2012-09-30 20:34:13 +08:00
|
|
|
model_fields = Model._meta.get_all_field_names()
|
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-30 00:42:49 +08:00
|
|
|
# Handle each field
|
2012-07-21 03:14:27 +08:00
|
|
|
for (field_name, field_value) in six.iteritems(d["fields"]):
|
2012-09-30 20:34:13 +08:00
|
|
|
|
|
|
|
if ignore and field_name not in model_fields:
|
|
|
|
# skip fields no longer on model
|
|
|
|
continue
|
|
|
|
|
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
|
|
|
if isinstance(field_value, str):
|
2012-07-21 16:00:10 +08:00
|
|
|
field_value = smart_text(field_value, options.get("encoding", settings.DEFAULT_CHARSET), strings_only=True)
|
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-30 00:42:49 +08:00
|
|
|
field = Model._meta.get_field(field_name)
|
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-02-27 01:33:27 +08:00
|
|
|
# Handle M2M relations
|
2006-06-30 00:42:49 +08:00
|
|
|
if field.rel and isinstance(field.rel, models.ManyToManyRel):
|
2009-12-14 20:39:20 +08:00
|
|
|
if hasattr(field.rel.to._default_manager, 'get_by_natural_key'):
|
|
|
|
def m2m_convert(value):
|
2012-08-08 23:59:31 +08:00
|
|
|
if hasattr(value, '__iter__') and not isinstance(value, six.text_type):
|
2009-12-22 23:18:51 +08:00
|
|
|
return field.rel.to._default_manager.db_manager(db).get_by_natural_key(*value).pk
|
2009-12-14 20:39:20 +08:00
|
|
|
else:
|
2012-07-21 16:00:10 +08:00
|
|
|
return smart_text(field.rel.to._meta.pk.to_python(value))
|
2009-12-14 20:39:20 +08:00
|
|
|
else:
|
2012-07-21 16:00:10 +08:00
|
|
|
m2m_convert = lambda v: smart_text(field.rel.to._meta.pk.to_python(v))
|
2009-12-14 20:39:20 +08:00
|
|
|
m2m_data[field.name] = [m2m_convert(pk) for pk in field_value]
|
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-30 00:42:49 +08:00
|
|
|
# Handle FK fields
|
2007-02-27 01:33:27 +08:00
|
|
|
elif field.rel and isinstance(field.rel, models.ManyToOneRel):
|
2008-08-29 03:38:56 +08:00
|
|
|
if field_value is not None:
|
2009-12-14 20:39:20 +08:00
|
|
|
if hasattr(field.rel.to._default_manager, 'get_by_natural_key'):
|
2012-08-08 23:59:31 +08:00
|
|
|
if hasattr(field_value, '__iter__') and not isinstance(field_value, six.text_type):
|
2009-12-22 23:18:51 +08:00
|
|
|
obj = field.rel.to._default_manager.db_manager(db).get_by_natural_key(*field_value)
|
2009-12-14 20:39:20 +08:00
|
|
|
value = getattr(obj, field.rel.field_name)
|
2010-03-18 23:22:15 +08:00
|
|
|
# If this is a natural foreign key to an object that
|
|
|
|
# has a FK/O2O as the foreign key, use the FK value
|
|
|
|
if field.rel.to._meta.pk.rel:
|
|
|
|
value = value.pk
|
2009-12-14 20:39:20 +08:00
|
|
|
else:
|
|
|
|
value = field.rel.to._meta.get_field(field.rel.field_name).to_python(field_value)
|
|
|
|
data[field.attname] = value
|
|
|
|
else:
|
|
|
|
data[field.attname] = field.rel.to._meta.get_field(field.rel.field_name).to_python(field_value)
|
2007-05-14 22:14:49 +08:00
|
|
|
else:
|
|
|
|
data[field.attname] = None
|
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-30 00:42:49 +08:00
|
|
|
# Handle all other fields
|
|
|
|
else:
|
|
|
|
data[field.name] = field.to_python(field_value)
|
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
|
|
|
|
2010-12-21 14:05:40 +08:00
|
|
|
yield base.DeserializedObject(Model(**data), m2m_data)
|
2006-06-30 00:42:49 +08:00
|
|
|
|
|
|
|
def _get_model(model_identifier):
|
|
|
|
"""
|
2013-02-05 17:16:07 +08:00
|
|
|
Helper to look up a model from an "app_label.model_name" string.
|
2006-06-30 00:42:49 +08:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
Model = models.get_model(*model_identifier.split("."))
|
|
|
|
except TypeError:
|
|
|
|
Model = None
|
|
|
|
if Model is None:
|
2012-06-08 00:08:47 +08:00
|
|
|
raise base.DeserializationError("Invalid model identifier: '%s'" % model_identifier)
|
2006-06-30 00:42:49 +08:00
|
|
|
return Model
|