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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.serializers import base
|
|
|
|
from django.db import models
|
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
|
|
|
from django.utils.encoding import smart_unicode
|
2006-06-30 00:42:49 +08:00
|
|
|
|
|
|
|
class Serializer(base.Serializer):
|
|
|
|
"""
|
|
|
|
Serializes a QuerySet to basic Python 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 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):
|
|
|
|
self.objects.append({
|
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
|
|
|
"model" : smart_unicode(obj._meta),
|
2007-09-15 13:32:29 +08:00
|
|
|
"pk" : smart_unicode(obj._get_pk_val(), strings_only=True),
|
2006-06-30 00:42:49 +08:00
|
|
|
"fields" : self._current
|
|
|
|
})
|
|
|
|
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
|
|
|
|
2006-06-30 00:42:49 +08:00
|
|
|
def handle_field(self, obj, field):
|
2007-09-15 13:32:29 +08:00
|
|
|
self._current[field.name] = smart_unicode(getattr(obj, field.name), 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
|
|
|
def handle_fk_field(self, obj, field):
|
|
|
|
related = getattr(obj, field.name)
|
|
|
|
if related is not None:
|
2007-05-28 13:41:32 +08:00
|
|
|
if field.rel.field_name == related._meta.pk.name:
|
|
|
|
# Related to remote object via primary key
|
|
|
|
related = related._get_pk_val()
|
|
|
|
else:
|
|
|
|
# Related to remote object via other field
|
|
|
|
related = getattr(related, field.rel.field_name)
|
2007-09-15 13:32:29 +08:00
|
|
|
self._current[field.name] = smart_unicode(related, 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
|
|
|
def handle_m2m_field(self, obj, field):
|
2007-09-15 13:32:29 +08:00
|
|
|
self._current[field.name] = [smart_unicode(related._get_pk_val(), strings_only=True)
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
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"])
|
2007-03-13 08:59:34 +08:00
|
|
|
data = {Model._meta.pk.attname : Model._meta.pk.to_python(d["pk"])}
|
2006-06-30 00:42:49 +08:00
|
|
|
m2m_data = {}
|
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
|
|
|
|
for (field_name, field_value) in d["fields"].iteritems():
|
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):
|
|
|
|
field_value = smart_unicode(field_value, options.get("encoding", settings.DEFAULT_CHARSET), strings_only=True)
|
|
|
|
|
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):
|
2007-03-13 08:59:34 +08:00
|
|
|
m2m_convert = field.rel.to._meta.pk.to_python
|
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
|
|
|
m2m_data[field.name] = [m2m_convert(smart_unicode(pk)) for pk in field_value]
|
|
|
|
|
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):
|
2007-05-14 22:14:49 +08:00
|
|
|
if field_value:
|
|
|
|
data[field.attname] = field.rel.to._meta.get_field(field.rel.field_name).to_python(field_value)
|
|
|
|
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
|
|
|
|
2006-06-30 00:42:49 +08:00
|
|
|
yield base.DeserializedObject(Model(**data), m2m_data)
|
|
|
|
|
|
|
|
def _get_model(model_identifier):
|
|
|
|
"""
|
|
|
|
Helper to look up a model from an "app_label.module_name" string.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
Model = models.get_model(*model_identifier.split("."))
|
|
|
|
except TypeError:
|
|
|
|
Model = None
|
|
|
|
if Model is 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
|
|
|
raise base.DeserializationError(u"Invalid model identifier: '%s'" % model_identifier)
|
2006-06-30 00:42:49 +08:00
|
|
|
return Model
|