Commit e7723683 authored by Preston Holmes's avatar Preston Holmes
Browse files

Fixed #9279 -- Added ignorenonexistent option to loaddata

Thanks to Roman Gladkov for the initial patch and Simon Charette for review.
parent 7cc4068c
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ try:
except ImportError:
    has_bz2 = False


class Command(BaseCommand):
    help = 'Installs the named fixture(s) in the database.'
    args = "fixture [fixture ...]"
@@ -31,9 +32,14 @@ class Command(BaseCommand):
        make_option('--database', action='store', dest='database',
            default=DEFAULT_DB_ALIAS, help='Nominates a specific database to load '
                'fixtures into. Defaults to the "default" database.'),
        make_option('--ignorenonexistent', '-i', action='store_true', dest='ignore',
            default=False, help='Ignores entries in the serialised data for fields'
                                ' that have been removed from the database'),
    )

    def handle(self, *fixture_labels, **options):

        ignore = options.get('ignore')
        using = options.get('database')

        connection = connections[using]
@@ -175,7 +181,7 @@ class Command(BaseCommand):
                                        self.stdout.write("Installing %s fixture '%s' from %s." % \
                                            (format, fixture_name, humanize(fixture_dir)))

                                    objects = serializers.deserialize(format, fixture, using=using)
                                    objects = serializers.deserialize(format, fixture, using=using, ignorenonexistent=ignore)

                                    for obj in objects:
                                        objects_in_fixture += 1
+11 −1
Original line number Diff line number Diff line
@@ -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"])}
        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)

+5 −0
Original line number Diff line number Diff line
@@ -289,6 +289,11 @@ Searches for and loads the contents of the named fixture into the database.
The :djadminopt:`--database` option can be used to specify the database
onto which the data will be loaded.

.. versionadded:: 1.5

The :djadminopt:`--ignorenonexistent` option can be used to ignore fields that
may have been removed from models since the fixture was originally generated.

What's a "fixture"?
~~~~~~~~~~~~~~~~~~~

+3 −0
Original line number Diff line number Diff line
@@ -195,6 +195,9 @@ Django 1.5 also includes several smaller improvements worth noting:
  whenever a user fails to login successfully. See
  :data:`~django.contrib.auth.signals.user_login_failed`

* The loaddata management command now supports an `ignorenonexistent` option to
  ignore data for fields that no longer exist.

Backwards incompatible changes in 1.5
=====================================

+8 −0
Original line number Diff line number Diff line
@@ -130,6 +130,14 @@ trust your data source you could just save the object and move on.

The Django object itself can be inspected as ``deserialized_object.object``.

.. versionadded:: 1.5

If fields in the serialized data do not exist on a model,
a ``DeserializationError`` will be raised unless the ``ignorenonexistent``
argument is passed in as True::

    serializers.deserialize("xml", data, ignorenonexistent=True)

.. _serialization-formats:

Serialization formats
Loading