1
0
mirror of https://github.com/django/django.git synced 2025-10-26 07:06:08 +00:00

Fixed #34420 -- Corrected the order of imports in generated migration files.

This commit is contained in:
Andy Chosak
2023-03-18 14:02:12 -04:00
committed by Mariusz Felisiak
parent 0eb3e9bd75
commit b295b31171
2 changed files with 11 additions and 2 deletions

View File

@@ -175,7 +175,10 @@ class MigrationWriter:
# 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])
# First group the "import" statements, then "from ... import ...".
sorted_imports = sorted(
imports, key=lambda i: (i.split()[0] == "from", i.split()[1])
)
items["imports"] = "\n".join(sorted_imports) + "\n" if imports else ""
if migration_imports:
items["imports"] += (