summaryrefslogtreecommitdiff
path: root/django/core/serializers/python.py
diff options
context:
space:
mode:
authorPreston Holmes <preston@ptone.com>2012-09-30 16:34:13 +0400
committerPreston Holmes <preston@ptone.com>2012-09-30 23:40:27 -0700
commite7723683dc652613df369d5ca412e8b1217012d3 (patch)
treeac531a7e8bdf7b363c9942951f9be8c5fefce85c /django/core/serializers/python.py
parent7cc4068c4470876c526830778cbdac2fdfd6dc26 (diff)
downloaddjango-e7723683dc652613df369d5ca412e8b1217012d3.tar.gz
Fixed #9279 -- Added ignorenonexistent option to loaddata
Thanks to Roman Gladkov for the initial patch and Simon Charette for review.
Diffstat (limited to 'django/core/serializers/python.py')
-rw-r--r--django/core/serializers/python.py12
1 files changed, 11 insertions, 1 deletions
diff --git a/django/core/serializers/python.py b/django/core/serializers/python.py
index a1fff6f9bb..37fa906280 100644
--- a/django/core/serializers/python.py
+++ b/django/core/serializers/python.py
@@ -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 import six
+
class Serializer(base.Serializer):
"""
Serializes a QuerySet to basic Python objects.
@@ -72,6 +73,7 @@ class Serializer(base.Serializer):
def getvalue(self):
return self.objects
+
def Deserializer(object_list, **options):
"""
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
"""
db = options.pop('using', DEFAULT_DB_ALIAS)
+ ignore = options.pop('ignorenonexistent', False)
+
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"])
- 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 = {}
+ model_fields = Model._meta.get_all_field_names()
# Handle each field
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):
field_value = smart_text(field_value, options.get("encoding", settings.DEFAULT_CHARSET), strings_only=True)