From a7bc00e17bebe34cdf23f744937b3c386ee73d81 Mon Sep 17 00:00:00 2001 From: Christopher Luc Date: Sat, 28 Mar 2015 17:09:44 -0700 Subject: [PATCH] Fixed #24514 -- Made migration writer omit models import if it's unused. --- django/db/migrations/writer.py | 12 ++++++++++-- tests/migrations/test_writer.py | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/django/db/migrations/writer.py b/django/db/migrations/writer.py index 4102b79697..a0832ff4f6 100644 --- a/django/db/migrations/writer.py +++ b/django/db/migrations/writer.py @@ -158,7 +158,7 @@ class MigrationWriter(object): "replaces_str": "", } - imports = {"from django.db import migrations, models"} + imports = set() # Deconstruct operations operations = [] @@ -188,7 +188,15 @@ class MigrationWriter(object): migration_imports.add(line.split("import")[1].strip()) imports.remove(line) self.needs_manual_porting = True - imports.discard("from django.db import models") + + # django.db.migrations is always used, but models import may not be. + # If models import exists, merge it with migrations import. + if "from django.db import models" in imports: + imports.discard("from django.db import models") + imports.add("from django.db import migrations, models") + else: + imports.add("from django.db import migrations") + # Sort imports by the package / module to be imported (the part after # "from" in "from ... import ..." or after "import" in "import ..."). sorted_imports = sorted(imports, key=lambda i: i.split()[1]) diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py index 46531df791..f0edfd3e30 100644 --- a/tests/migrations/test_writer.py +++ b/tests/migrations/test_writer.py @@ -512,6 +512,22 @@ class WriterTests(TestCase): output ) + def test_models_import_omitted(self): + """ + django.db.models shouldn't be imported if unused. + """ + migration = type(str("Migration"), (migrations.Migration,), { + "operations": [ + migrations.AlterModelOptions( + name='model', + options={'verbose_name': 'model', 'verbose_name_plural': 'models'}, + ), + ] + }) + writer = MigrationWriter(migration) + output = writer.as_string().decode('utf-8') + self.assertIn("from django.db import migrations\n", output) + def test_deconstruct_class_arguments(self): # Yes, it doesn't make sense to use a class as a default for a # CharField. It does make sense for custom fields though, for example