From fde6fb28984a76d5bee794f4c0458eb25fe56fcd Mon Sep 17 00:00:00 2001 From: abhiabhi94 <13880786+abhiabhi94@users.noreply.github.com> Date: Thu, 1 Jul 2021 12:58:02 +0530 Subject: [PATCH] Fixed #32893 -- Fixed serialization of models.Model class in migrations. Migrations assumed that an import of the models.Model class must already be included when it's serialized, but for models with only custom fields this was not necessarily the case. Thanks Jaap Joris Vens for the report. --- django/db/migrations/serializer.py | 2 +- tests/migrations/test_writer.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/django/db/migrations/serializer.py b/django/db/migrations/serializer.py index e19c881cda5..6e78462e95d 100644 --- a/django/db/migrations/serializer.py +++ b/django/db/migrations/serializer.py @@ -273,7 +273,7 @@ class TupleSerializer(BaseSequenceSerializer): class TypeSerializer(BaseSerializer): def serialize(self): special_cases = [ - (models.Model, "models.Model", []), + (models.Model, "models.Model", ['from django.db import models']), (type(None), 'type(None)', []), ] for case, string, imports in special_cases: diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py index a590ff4398a..471194ffc22 100644 --- a/tests/migrations/test_writer.py +++ b/tests/migrations/test_writer.py @@ -658,6 +658,13 @@ class WriterTests(SimpleTestCase): def test_serialize_type_none(self): self.assertSerializedEqual(type(None)) + def test_serialize_type_model(self): + self.assertSerializedEqual(models.Model) + self.assertSerializedResultEqual( + MigrationWriter.serialize(models.Model), + ("('models.Model', {'from django.db import models'})", set()), + ) + def test_simple_migration(self): """ Tests serializing a simple migration.