Fixed #31762 -- Made reloading the database for tests use the base manager.

Co-authored-by: Eugene Kulak <kulak.eugene@gmail.com>
This commit is contained in:
Hasan Ramezani 2020-11-10 23:22:30 +01:00 committed by Mariusz Felisiak
parent c9e8544321
commit 50c3ac6fa9
3 changed files with 21 additions and 1 deletions

View File

@ -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()

View File

@ -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)

View File

@ -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)