Code

Ticket #15206: django-15206-1.3.patch

File django-15206-1.3.patch, 3.7 KB (added by acdha, 3 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): 
    2020    ordering = ('name',) 
    2121    filter_horizontal = ('permissions',) 
    2222 
     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 
    2332class UserAdmin(admin.ModelAdmin): 
    2433    add_form_template = 'admin/auth/user/add_form.html' 
    2534    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 
    1111from django.core.urlresolvers import reverse 
    1212# Register auth models with the admin. 
    1313from django.contrib.auth import REDIRECT_FIELD_NAME, admin 
    14 from django.contrib.auth.models import User, Permission, UNUSABLE_PASSWORD 
     14from django.contrib.auth.models import Group, User, Permission, UNUSABLE_PASSWORD 
    1515from django.contrib.contenttypes.models import ContentType 
    1616from django.contrib.admin.models import LogEntry, DELETION 
    1717from django.contrib.admin.sites import LOGIN_FORM_KEY 
    class UserAdminTest(TestCase): 
    25272527        self.assertEqual(User.objects.count(), user_count + 1) 
    25282528        self.assertNotEqual(new_user.password, UNUSABLE_PASSWORD) 
    25292529 
     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 
     2540class 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 
    25302573try: 
    25312574    import docutils 
    25322575except ImportError: