From 76f2f58a1853fd7a774f3df3fe454022a0206ff9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrno=20Ader?= Date: Thu, 10 Jul 2014 17:00:36 +0300 Subject: [PATCH] Fixed #22956 -- Made PermissionManager.get_by_natural_key() use the correct database for content type lookup. --- django/contrib/auth/models.py | 3 +- django/contrib/auth/tests/test_models.py | 61 +++++++++++++++++++++++- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py index 5ee1699880..daa71b9fc9 100644 --- a/django/contrib/auth/models.py +++ b/django/contrib/auth/models.py @@ -32,8 +32,7 @@ class PermissionManager(models.Manager): def get_by_natural_key(self, codename, app_label, model): return self.get( codename=codename, - content_type=ContentType.objects.get_by_natural_key(app_label, - model), + content_type=ContentType.objects.db_manager(self.db).get_by_natural_key(app_label, model), ) diff --git a/django/contrib/auth/tests/test_models.py b/django/contrib/auth/tests/test_models.py index 9e2c653738..ea0cfb5528 100644 --- a/django/contrib/auth/tests/test_models.py +++ b/django/contrib/auth/tests/test_models.py @@ -1,6 +1,7 @@ from django.contrib.auth import get_user_model -from django.contrib.auth.models import AbstractUser, Group, User, UserManager +from django.contrib.auth.models import AbstractUser, Group, Permission, User, UserManager from django.contrib.auth.tests.utils import skipIfCustomUser +from django.contrib.contenttypes.models import ContentType from django.core import mail from django.db.models.signals import post_save from django.test import TestCase, override_settings @@ -43,6 +44,64 @@ class LoadDataWithNaturalKeysTestCase(TestCase): self.assertEqual(group, user.groups.get()) +class LoadDataWithNaturalKeysAndMultipleDatabasesTestCase(TestCase): + multi_db = True + + def test_load_data_with_user_permissions(self): + # Create test contenttypes for both databases + default_objects = [ + ContentType.objects.db_manager('default').create( + model='examplemodela', + name='example model a', + app_label='app_a', + ), + ContentType.objects.db_manager('default').create( + model='examplemodelb', + name='example model b', + app_label='app_b', + ), + ] + other_objects = [ + ContentType.objects.db_manager('other').create( + model='examplemodelb', + name='example model b', + app_label='app_b', + ), + ContentType.objects.db_manager('other').create( + model='examplemodela', + name='example model a', + app_label='app_a', + ), + ] + + # Now we create the test UserPermission + Permission.objects.db_manager("default").create( + name="Can delete example model b", + codename="delete_examplemodelb", + content_type=default_objects[1], + ) + Permission.objects.db_manager("other").create( + name="Can delete example model b", + codename="delete_examplemodelb", + content_type=other_objects[0], + ) + + perm_default = Permission.objects.get_by_natural_key( + 'delete_examplemodelb', + 'app_b', + 'examplemodelb', + ) + + perm_other = Permission.objects.db_manager('other').get_by_natural_key( + 'delete_examplemodelb', + 'app_b', + 'examplemodelb', + ) + + self.assertEqual(perm_default.content_type_id, default_objects[1].id) + self.assertEqual(perm_other.content_type_id, other_objects[0].id) + + @skipIfCustomUser class UserManagerTestCase(TestCase):