diff --git a/django/core/management/commands/loaddata.py b/django/core/management/commands/loaddata.py index 8b8ffa5512..5216e1522c 100644 --- a/django/core/management/commands/loaddata.py +++ b/django/core/management/commands/loaddata.py @@ -132,15 +132,17 @@ class Command(BaseCommand): return else: fixture_count += 1 + objects_in_fixture = 0 if verbosity > 0: print "Installing %s fixture '%s' from %s." % \ (format, fixture_name, humanize(fixture_dir)) try: objects = serializers.deserialize(format, fixture) for obj in objects: - object_count += 1 + objects_in_fixture += 1 models.add(obj.object.__class__) obj.save() + object_count += objects_in_fixture label_found = True except (SystemExit, KeyboardInterrupt): raise @@ -158,6 +160,17 @@ class Command(BaseCommand): (full_path, traceback.format_exc()))) return fixture.close() + + # If the fixture we loaded contains 0 objects, assume that an + # error was encountered during fixture loading. + if objects_in_fixture == 0: + sys.stderr.write( + self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)" % + (fixture_name))) + transaction.rollback() + transaction.leave_transaction_management() + return + except Exception, e: if verbosity > 1: print "No %s fixture '%s' in %s." % \