diff --git a/django/db/migrations/writer.py b/django/db/migrations/writer.py
index a59f0c8dcb..f0eb4d2dc1 100644
a
|
b
|
class MigrationWriter:
|
175 | 175 | |
176 | 176 | # Sort imports by the package / module to be imported (the part after |
177 | 177 | # "from" in "from ... import ..." or after "import" in "import ..."). |
178 | | sorted_imports = sorted(imports, key=lambda i: i.split()[1]) |
| 178 | sorted_imports = sorted( |
| 179 | imports, key=lambda i: (i.split()[0] == "from", i.split()[1]) |
| 180 | ) |
179 | 181 | items["imports"] = "\n".join(sorted_imports) + "\n" if imports else "" |
180 | 182 | if migration_imports: |
181 | 183 | items["imports"] += ( |
diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py
index 1c11c68e97..c6638b3083 100644
a
|
b
|
import os
|
7 | 7 | import pathlib |
8 | 8 | import re |
9 | 9 | import sys |
| 10 | import time |
10 | 11 | import uuid |
11 | 12 | import zoneinfo |
12 | 13 | from types import NoneType |
… |
… |
class WriterTests(SimpleTestCase):
|
912 | 913 | ), |
913 | 914 | ), |
914 | 915 | ), |
| 916 | migrations.AddField( |
| 917 | "mymodel", |
| 918 | "myfield2", |
| 919 | models.FloatField(default=time.time), |
| 920 | ), |
915 | 921 | ] |
916 | 922 | }, |
917 | 923 | ) |
918 | 924 | writer = MigrationWriter(migration) |
919 | 925 | output = writer.as_string() |
920 | 926 | self.assertIn( |
921 | | "import datetime\nfrom django.db import migrations, models\n", |
| 927 | "import datetime\nimport time\nfrom django.db import migrations, models\n", |
922 | 928 | output, |
923 | 929 | ) |
924 | 930 | |