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.
|
|
|
|
"""
|
2015-04-02 03:42:09 +08:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.apps import apps
|
2006-06-30 00:42:49 +08:00
|
|
|
from django.core.serializers import base
|
2015-01-07 08:16:35 +08:00
|
|
|
from django.db import DEFAULT_DB_ALIAS, models
|
2017-06-05 18:37:56 +08:00
|
|
|
from django.utils.encoding import is_protected_type
|
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):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Serialize a QuerySet to basic Python objects.
|
2006-06-30 00:42:49 +08:00
|
|
|
"""
|
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):
|
2015-04-02 03:42:09 +08:00
|
|
|
self._current = OrderedDict()
|
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):
|
2017-06-05 18:37:56 +08:00
|
|
|
data = OrderedDict([('model', str(obj._meta))])
|
2012-08-01 09:49:01 +08:00
|
|
|
if not self.use_natural_primary_keys or not hasattr(obj, 'natural_key'):
|
2017-06-05 18:37:56 +08:00
|
|
|
data["pk"] = self._value_from_field(obj, obj._meta.pk)
|
2015-04-02 03:42:09 +08:00
|
|
|
data['fields'] = self._current
|
2012-08-01 09:49:01 +08:00
|
|
|
return data
|
2012-05-26 17:43:37 +08:00
|
|
|
|
2017-06-05 18:37:56 +08:00
|
|
|
def _value_from_field(self, obj, field):
|
2015-04-26 14:30:46 +08:00
|
|
|
value = field.value_from_object(obj)
|
2009-04-13 20:35:49 +08:00
|
|
|
# Protected types (i.e., primitives like None, numbers, dates,
|
|
|
|
# and Decimals) are passed through as is. All other values are
|
|
|
|
# converted to string first.
|
2017-06-05 18:37:56 +08:00
|
|
|
return value if is_protected_type(value) else field.value_to_string(obj)
|
|
|
|
|
|
|
|
def handle_field(self, obj, field):
|
|
|
|
self._current[field.name] = self._value_from_field(obj, field)
|
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):
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.use_natural_foreign_keys and hasattr(field.remote_field.model, 'natural_key'):
|
2012-02-05 02:27:07 +08:00
|
|
|
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:
|
2017-06-05 18:37:56 +08:00
|
|
|
value = self._value_from_field(obj, field)
|
2012-02-05 02:27:07 +08:00
|
|
|
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):
|
2015-02-26 22:19:17 +08:00
|
|
|
if field.remote_field.through._meta.auto_created:
|
|
|
|
if self.use_natural_foreign_keys and hasattr(field.remote_field.model, 'natural_key'):
|
2016-01-24 00:47:07 +08:00
|
|
|
def m2m_value(value):
|
|
|
|
return value.natural_key()
|
2009-12-14 20:39:20 +08:00
|
|
|
else:
|
2016-01-24 00:47:07 +08:00
|
|
|
def m2m_value(value):
|
2017-06-05 18:37:56 +08:00
|
|
|
return self._value_from_field(value, value._meta.pk)
|
2016-03-29 06:33:29 +08:00
|
|
|
self._current[field.name] = [
|
|
|
|
m2m_value(related) 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
|
|
|
|
2017-02-02 00:41:56 +08:00
|
|
|
def Deserializer(object_list, *, using=DEFAULT_DB_ALIAS, ignorenonexistent=False, **options):
|
2006-06-30 00:42:49 +08:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
"""
|
2018-07-14 05:54:47 +08:00
|
|
|
handle_forward_references = options.pop('handle_forward_references', False)
|
2015-10-23 10:29:46 +08:00
|
|
|
field_names_cache = {} # Model: <list of field_names>
|
2012-09-30 20:34:13 +08:00
|
|
|
|
2006-06-30 00:42:49 +08:00
|
|
|
for d in object_list:
|
|
|
|
# Look up the model and starting build a dict of data for it.
|
2014-02-21 14:43:08 +08:00
|
|
|
try:
|
|
|
|
Model = _get_model(d["model"])
|
2014-05-19 05:42:03 +08:00
|
|
|
except base.DeserializationError:
|
2017-02-02 00:41:56 +08:00
|
|
|
if ignorenonexistent:
|
2014-02-21 14:43:08 +08:00
|
|
|
continue
|
|
|
|
else:
|
2014-05-19 05:42:03 +08:00
|
|
|
raise
|
2012-08-01 09:49:01 +08:00
|
|
|
data = {}
|
|
|
|
if 'pk' in d:
|
2015-03-27 02:31:09 +08:00
|
|
|
try:
|
|
|
|
data[Model._meta.pk.attname] = Model._meta.pk.to_python(d.get('pk'))
|
|
|
|
except Exception as e:
|
|
|
|
raise base.DeserializationError.WithData(e, d['model'], d.get('pk'), None)
|
2006-06-30 00:42:49 +08:00
|
|
|
m2m_data = {}
|
2018-07-14 05:54:47 +08:00
|
|
|
deferred_fields = {}
|
2015-10-23 10:29:46 +08:00
|
|
|
|
|
|
|
if Model not in field_names_cache:
|
|
|
|
field_names_cache[Model] = {f.name for f in Model._meta.get_fields()}
|
|
|
|
field_names = field_names_cache[Model]
|
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
|
2017-01-07 19:11:46 +08:00
|
|
|
for (field_name, field_value) in d["fields"].items():
|
2012-09-30 20:34:13 +08:00
|
|
|
|
2017-02-02 00:41:56 +08:00
|
|
|
if ignorenonexistent and field_name not in field_names:
|
2012-09-30 20:34:13 +08:00
|
|
|
# skip fields no longer on model
|
|
|
|
continue
|
|
|
|
|
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
|
2015-02-26 22:19:17 +08:00
|
|
|
if field.remote_field and isinstance(field.remote_field, models.ManyToManyRel):
|
2015-03-27 02:31:09 +08:00
|
|
|
try:
|
2018-07-14 05:54:47 +08:00
|
|
|
values = base.deserialize_m2m_values(field, field_value, using, handle_forward_references)
|
2018-03-21 00:07:39 +08:00
|
|
|
except base.M2MDeserializationError as e:
|
|
|
|
raise base.DeserializationError.WithData(e.original_exc, d['model'], d.get('pk'), e.pk)
|
2018-07-14 05:54:47 +08:00
|
|
|
if values == base.DEFER_FIELD:
|
|
|
|
deferred_fields[field] = field_value
|
|
|
|
else:
|
|
|
|
m2m_data[field.name] = values
|
2006-06-30 00:42:49 +08:00
|
|
|
# Handle FK fields
|
2015-02-26 22:19:17 +08:00
|
|
|
elif field.remote_field and isinstance(field.remote_field, models.ManyToOneRel):
|
2018-03-21 00:07:39 +08:00
|
|
|
try:
|
2018-07-14 05:54:47 +08:00
|
|
|
value = base.deserialize_fk_value(field, field_value, using, handle_forward_references)
|
2018-03-21 00:07:39 +08:00
|
|
|
except Exception as e:
|
|
|
|
raise base.DeserializationError.WithData(e, d['model'], d.get('pk'), field_value)
|
2018-07-14 05:54:47 +08:00
|
|
|
if value == base.DEFER_FIELD:
|
|
|
|
deferred_fields[field] = field_value
|
|
|
|
else:
|
|
|
|
data[field.attname] = value
|
2006-06-30 00:42:49 +08:00
|
|
|
# Handle all other fields
|
|
|
|
else:
|
2015-03-27 02:31:09 +08:00
|
|
|
try:
|
|
|
|
data[field.name] = field.to_python(field_value)
|
|
|
|
except Exception as e:
|
|
|
|
raise base.DeserializationError.WithData(e, d['model'], d.get('pk'), 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
|
|
|
|
2017-02-02 00:41:56 +08:00
|
|
|
obj = base.build_instance(Model, data, using)
|
2018-07-14 05:54:47 +08:00
|
|
|
yield base.DeserializedObject(obj, m2m_data, deferred_fields)
|
2006-06-30 00:42:49 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2006-06-30 00:42:49 +08:00
|
|
|
def _get_model(model_identifier):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""Look up a model from an "app_label.model_name" string."""
|
2006-06-30 00:42:49 +08:00
|
|
|
try:
|
2014-01-26 19:57:08 +08:00
|
|
|
return apps.get_model(model_identifier)
|
2013-12-28 21:55:54 +08:00
|
|
|
except (LookupError, TypeError):
|
2012-06-08 00:08:47 +08:00
|
|
|
raise base.DeserializationError("Invalid model identifier: '%s'" % model_identifier)
|