Fixed #24514 -- Made migration writer omit models import if it's unused.
This commit is contained in:
parent
d5d92260bb
commit
a7bc00e17b
|
@ -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
|
||||
|
||||
# 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])
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue