1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Fixed #34165 -- Made permissions creation respect the "using" parameter.

This commit is contained in:
David Wobrock 2022-12-23 18:16:05 +01:00 committed by Mariusz Felisiak
parent 0bd2c0c901
commit 5aaad5f39c
2 changed files with 29 additions and 5 deletions

View File

@ -95,11 +95,16 @@ def create_permissions(
.values_list("content_type", "codename")
)
perms = [
Permission(codename=codename, name=name, content_type=ct)
for ct, (codename, name) in searched_perms
if (ct.pk, codename) not in all_perms
]
perms = []
for ct, (codename, name) in searched_perms:
if (ct.pk, codename) not in all_perms:
permission = Permission()
permission._state.db = using
permission.codename = codename
permission.name = name
permission.content_type = ct
perms.append(permission)
Permission.objects.using(using).bulk_create(perms)
if verbosity >= 2:
for perm in perms:

View File

@ -1485,3 +1485,22 @@ class CreatePermissionsTests(TestCase):
codename=codename,
).exists()
)
class DefaultDBRouter:
"""Route all writes to default."""
def db_for_write(self, model, **hints):
return "default"
@override_settings(DATABASE_ROUTERS=[DefaultDBRouter()])
class CreatePermissionsMultipleDatabasesTests(TestCase):
databases = {"default", "other"}
def test_set_permissions_fk_to_using_parameter(self):
Permission.objects.using("other").delete()
with self.assertNumQueries(6, using="other") as captured_queries:
create_permissions(apps.get_app_config("auth"), verbosity=0, using="other")
self.assertIn("INSERT INTO", captured_queries[-1]["sql"].upper())
self.assertGreater(Permission.objects.using("other").count(), 0)