Ticket #15206: django-15206-1.3.patch
File django-15206-1.3.patch, 3.7 KB (added by , 14 years ago) |
---|
-
django/contrib/auth/admin.py
From 315e334d1efd86ceb8ab443f8f9125ff367564ed Mon Sep 17 00:00:00 2001 From: Chris Adams <chris@improbable.org> Date: Tue, 1 Feb 2011 15:05:34 -0500 Subject: [PATCH] contrib.auth: use select_related on group change_form This avoids making a query for each permission loading its content_type --- django/contrib/auth/admin.py | 9 +++++ tests/regressiontests/admin_views/tests.py | 45 +++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletions(-) diff --git a/django/contrib/auth/admin.py b/django/contrib/auth/admin.py index 497e9b9..683b0af 100644
a b class GroupAdmin(admin.ModelAdmin): 20 20 ordering = ('name',) 21 21 filter_horizontal = ('permissions',) 22 22 23 def formfield_for_manytomany(self, db_field, request=None, **kwargs): 24 if db_field.name == "permissions": 25 qs = kwargs.get("queryset", db_field.rel.to.objects) 26 # Avoid a major performance hit resolving permission names which 27 # triggers a content_type load: 28 kwargs['queryset'] = qs.select_related("content_type") 29 30 return super(GroupAdmin, self).formfield_for_manytomany(db_field, request=None, **kwargs) 31 23 32 class UserAdmin(admin.ModelAdmin): 24 33 add_form_template = 'admin/auth/user/add_form.html' 25 34 change_user_password_template = None -
tests/regressiontests/admin_views/tests.py
diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index d320db5..62bf419 100644
a b from django.core.files import temp as tempfile 11 11 from django.core.urlresolvers import reverse 12 12 # Register auth models with the admin. 13 13 from django.contrib.auth import REDIRECT_FIELD_NAME, admin 14 from django.contrib.auth.models import User, Permission, UNUSABLE_PASSWORD14 from django.contrib.auth.models import Group, User, Permission, UNUSABLE_PASSWORD 15 15 from django.contrib.contenttypes.models import ContentType 16 16 from django.contrib.admin.models import LogEntry, DELETION 17 17 from django.contrib.admin.sites import LOGIN_FORM_KEY … … class UserAdminTest(TestCase): 2527 2527 self.assertEqual(User.objects.count(), user_count + 1) 2528 2528 self.assertNotEqual(new_user.password, UNUSABLE_PASSWORD) 2529 2529 2530 def test_user_permission_performance(self): 2531 u = User.objects.all()[0] 2532 2533 def test_f(): 2534 response = self.client.get('/test_admin/admin/auth/user/%s/' % u.pk) 2535 self.assertEqual(response.status_code, 200) 2536 2537 self.assertNumQueries(8, test_f) 2538 2539 2540 class GroupAdminTest(TestCase): 2541 """ 2542 Tests group CRUD functionality. 2543 """ 2544 fixtures = ['admin-views-users.xml'] 2545 2546 def setUp(self): 2547 self.client.login(username='super', password='secret') 2548 2549 def tearDown(self): 2550 self.client.logout() 2551 2552 def test_save_button(self): 2553 group_count = Group.objects.count() 2554 response = self.client.post('/test_admin/admin/auth/group/add/', { 2555 'name': 'newgroup', 2556 }) 2557 2558 new_group = Group.objects.order_by('-id')[0] 2559 self.assertRedirects(response, '/test_admin/admin/auth/group/') 2560 self.assertEqual(Group.objects.count(), group_count + 1) 2561 2562 def test_group_permission_performance(self): 2563 g = Group.objects.create(name="test_group") 2564 2565 # If only we didn't care about Python 2.4: 2566 def test_f(): 2567 response = self.client.get('/test_admin/admin/auth/group/%s/' % g.pk) 2568 self.assertEqual(response.status_code, 200) 2569 2570 self.assertNumQueries(7, test_f) 2571 2572 2530 2573 try: 2531 2574 import docutils 2532 2575 except ImportError: