From 50c3ac6fa9b7c8a94a6d1dc87edf775e3bc4d575 Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Tue, 10 Nov 2020 23:22:30 +0100 Subject: [PATCH] Fixed #31762 -- Made reloading the database for tests use the base manager. Co-authored-by: Eugene Kulak --- django/db/backends/base/creation.py | 2 +- tests/backends/base/test_creation.py | 13 +++++++++++++ tests/backends/models.py | 7 +++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/django/db/backends/base/creation.py b/django/db/backends/base/creation.py index 48a4a6994e5..0099dd18bb8 100644 --- a/django/db/backends/base/creation.py +++ b/django/db/backends/base/creation.py @@ -122,7 +122,7 @@ class BaseDatabaseCreation: model._meta.can_migrate(self.connection) and router.allow_migrate_model(self.connection.alias, model) ): - queryset = model._default_manager.using( + queryset = model._base_manager.using( self.connection.alias, ).order_by(model._meta.pk.name) yield from queryset.iterator() diff --git a/tests/backends/base/test_creation.py b/tests/backends/base/test_creation.py index 3d22cabd0fc..3e29961d21c 100644 --- a/tests/backends/base/test_creation.py +++ b/tests/backends/base/test_creation.py @@ -1,4 +1,5 @@ import copy +import datetime from unittest import mock from django.db import DEFAULT_DB_ALIAS, connection, connections @@ -10,6 +11,7 @@ from django.test.utils import override_settings from ..models import ( CircularA, CircularB, Object, ObjectReference, ObjectSelfReference, + SchoolClass, ) @@ -175,3 +177,14 @@ class TestDeserializeDbFromString(TransactionTestCase): obj_b = CircularB.objects.get() self.assertEqual(obj_a.obj, obj_b) self.assertEqual(obj_b.obj, obj_a) + + def test_serialize_db_to_string_base_manager(self): + SchoolClass.objects.create(year=1000, last_updated=datetime.datetime.now()) + with mock.patch('django.db.migrations.loader.MigrationLoader') as loader: + # serialize_db_to_string() serializes only migrated apps, so mark + # the backends app as migrated. + loader_instance = loader.return_value + loader_instance.migrated_apps = {'backends'} + data = connection.creation.serialize_db_to_string() + self.assertIn('"model": "backends.schoolclass"', data) + self.assertIn('"year": 1000', data) diff --git a/tests/backends/models.py b/tests/backends/models.py index 096fdb57cc5..5368e8d9038 100644 --- a/tests/backends/models.py +++ b/tests/backends/models.py @@ -21,11 +21,18 @@ class Person(models.Model): return '%s %s' % (self.first_name, self.last_name) +class SchoolClassManager(models.Manager): + def get_queryset(self): + return super().get_queryset().exclude(year=1000) + + class SchoolClass(models.Model): year = models.PositiveIntegerField() day = models.CharField(max_length=9, blank=True) last_updated = models.DateTimeField() + objects = SchoolClassManager() + class VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ(models.Model): primary_key_is_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.AutoField(primary_key=True)