mirror of
https://github.com/django/django.git
synced 2025-01-03 15:06:09 +00:00
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": "",
|
"replaces_str": "",
|
||||||
}
|
}
|
||||||
|
|
||||||
imports = {"from django.db import migrations, models"}
|
imports = set()
|
||||||
|
|
||||||
# Deconstruct operations
|
# Deconstruct operations
|
||||||
operations = []
|
operations = []
|
||||||
@ -188,7 +188,15 @@ class MigrationWriter(object):
|
|||||||
migration_imports.add(line.split("import")[1].strip())
|
migration_imports.add(line.split("import")[1].strip())
|
||||||
imports.remove(line)
|
imports.remove(line)
|
||||||
self.needs_manual_porting = True
|
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
|
# Sort imports by the package / module to be imported (the part after
|
||||||
# "from" in "from ... import ..." or after "import" in "import ...").
|
# "from" in "from ... import ..." or after "import" in "import ...").
|
||||||
sorted_imports = sorted(imports, key=lambda i: i.split()[1])
|
sorted_imports = sorted(imports, key=lambda i: i.split()[1])
|
||||||
|
@ -512,6 +512,22 @@ class WriterTests(TestCase):
|
|||||||
output
|
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):
|
def test_deconstruct_class_arguments(self):
|
||||||
# Yes, it doesn't make sense to use a class as a default for a
|
# 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
|
# CharField. It does make sense for custom fields though, for example
|
||||||
|
Loading…
Reference in New Issue
Block a user