diff --git a/django/db/migrations/operations/models.py b/django/db/migrations/operations/models.py index 90fc31bee5..8a5c04393b 100644 --- a/django/db/migrations/operations/models.py +++ b/django/db/migrations/operations/models.py @@ -170,6 +170,19 @@ class CreateModel(ModelOperation): managers=self.managers, ), ] + elif ( + isinstance(operation, AlterModelManagers) + and self.name_lower == operation.name_lower + ): + return [ + CreateModel( + self.name, + fields=self.fields, + options=self.options, + bases=self.bases, + managers=operation.managers, + ), + ] elif ( isinstance(operation, AlterTogetherOptionOperation) and self.name_lower == operation.name_lower diff --git a/tests/migrations/test_optimizer.py b/tests/migrations/test_optimizer.py index 59dd401a3c..6bde378cd9 100644 --- a/tests/migrations/test_optimizer.py +++ b/tests/migrations/test_optimizer.py @@ -129,6 +129,30 @@ class OptimizerTests(SimpleTestCase): ], ) + def test_create_alter_model_managers(self): + self.assertOptimizesTo( + [ + migrations.CreateModel("Foo", fields=[]), + migrations.AlterModelManagers( + name="Foo", + managers=[ + ("objects", models.Manager()), + ("things", models.Manager()), + ], + ), + ], + [ + migrations.CreateModel( + "Foo", + fields=[], + managers=[ + ("objects", models.Manager()), + ("things", models.Manager()), + ], + ), + ], + ) + def test_create_model_and_remove_model_options(self): self.assertOptimizesTo( [