|
@@ -11,6 +11,7 @@ from django.db import models, DEFAULT_DB_ALIAS
|
|
from django.utils.encoding import smart_text, is_protected_type
|
|
from django.utils.encoding import smart_text, is_protected_type
|
|
from django.utils import six
|
|
from django.utils import six
|
|
|
|
|
|
|
|
+
|
|
class Serializer(base.Serializer):
|
|
class Serializer(base.Serializer):
|
|
"""
|
|
"""
|
|
Serializes a QuerySet to basic Python objects.
|
|
Serializes a QuerySet to basic Python objects.
|
|
@@ -72,6 +73,7 @@ class Serializer(base.Serializer):
|
|
def getvalue(self):
|
|
def getvalue(self):
|
|
return self.objects
|
|
return self.objects
|
|
|
|
|
|
|
|
+
|
|
def Deserializer(object_list, **options):
|
|
def Deserializer(object_list, **options):
|
|
"""
|
|
"""
|
|
Deserialize simple Python objects back into Django ORM instances.
|
|
Deserialize simple Python objects back into Django ORM instances.
|
|
@@ -80,15 +82,23 @@ def Deserializer(object_list, **options):
|
|
stream or a string) to the constructor
|
|
stream or a string) to the constructor
|
|
"""
|
|
"""
|
|
db = options.pop('using', DEFAULT_DB_ALIAS)
|
|
db = options.pop('using', DEFAULT_DB_ALIAS)
|
|
|
|
+ ignore = options.pop('ignorenonexistent', False)
|
|
|
|
+
|
|
models.get_apps()
|
|
models.get_apps()
|
|
for d in object_list:
|
|
for d in object_list:
|
|
# Look up the model and starting build a dict of data for it.
|
|
# Look up the model and starting build a dict of data for it.
|
|
Model = _get_model(d["model"])
|
|
Model = _get_model(d["model"])
|
|
- data = {Model._meta.pk.attname : Model._meta.pk.to_python(d["pk"])}
|
|
|
|
|
|
+ data = {Model._meta.pk.attname: Model._meta.pk.to_python(d["pk"])}
|
|
m2m_data = {}
|
|
m2m_data = {}
|
|
|
|
+ model_fields = Model._meta.get_all_field_names()
|
|
|
|
|
|
# Handle each field
|
|
# Handle each field
|
|
for (field_name, field_value) in six.iteritems(d["fields"]):
|
|
for (field_name, field_value) in six.iteritems(d["fields"]):
|
|
|
|
+
|
|
|
|
+ if ignore and field_name not in model_fields:
|
|
|
|
+ # skip fields no longer on model
|
|
|
|
+ continue
|
|
|
|
+
|
|
if isinstance(field_value, str):
|
|
if isinstance(field_value, str):
|
|
field_value = smart_text(field_value, options.get("encoding", settings.DEFAULT_CHARSET), strings_only=True)
|
|
field_value = smart_text(field_value, options.get("encoding", settings.DEFAULT_CHARSET), strings_only=True)
|
|
|
|
|