1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #24514 -- Made migration writer omit models import if it's unused.

This commit is contained in:
Christopher Luc
2015-03-28 17:09:44 -07:00
committed by Tim Graham
parent d5d92260bb
commit a7bc00e17b
2 changed files with 26 additions and 2 deletions

View File

@@ -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])