Ticket #31472: fix.patch

File fix.patch, 2.6 KB (added by krnr, 5 years ago)
  • django/contrib/auth/admin.py

    diff --git a/django/contrib/auth/admin.py b/django/contrib/auth/admin.py
    index 6709e63..75ebc9b 100644
    a b class UserAdmin(admin.ModelAdmin):  
    6565    filter_horizontal = ('groups', 'user_permissions',)
    6666
    6767    def get_fieldsets(self, request, obj=None):
    68         if not obj:
     68        if obj is None:
    6969            return self.add_fieldsets
    7070        return super().get_fieldsets(request, obj)
    7171
  • tests/modeladmin/tests.py

    diff --git a/tests/modeladmin/tests.py b/tests/modeladmin/tests.py
    index 6934bf2..ceb8123 100644
    a b from django.contrib.admin.widgets import (  
    1111    AdminDateWidget, AdminRadioSelect, AutocompleteSelect,
    1212    AutocompleteSelectMultiple,
    1313)
     14from django.contrib.auth.admin import UserAdmin
    1415from django.contrib.auth.models import User
    1516from django.db import models
    1617from django.forms.widgets import Select
    class ModelAdminTests(TestCase):  
    6768        self.assertEqual(ma.get_fieldsets(request), [(None, {'fields': ['name', 'bio', 'sign_date']})])
    6869        self.assertEqual(ma.get_fieldsets(request, self.band), [(None, {'fields': ['name', 'bio', 'sign_date']})])
    6970
     71    def test_default_fieldsets_useradmin(self):
     72        # Here's the default case. There are no custom form_add/form_change methods,
     73        # no fields argument, and no fieldsets argument.
     74        class CustomUser(User):
     75            is_deleted = models.BooleanField(default=False)
     76
     77            def __bool__(self):
     78                return not self.is_deleted
     79
     80        ua = UserAdmin(CustomUser, self.site)
     81        user = CustomUser()
     82
     83        add_fieldsets = ((None, {
     84            'classes': ('wide',), 'fields': ('username', 'password1', 'password2')
     85        }),)
     86        change_fieldsets = (
     87            (None, {'fields': ('username', 'password')}),
     88            ('Personal info', {'fields': ('first_name', 'last_name', 'email')}),
     89            ('Permissions', {'fields':
     90                ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')
     91            }),
     92            ('Important dates', {'fields': ('last_login', 'date_joined')}),
     93        )
     94
     95        self.assertEqual(ua.get_fieldsets(request), add_fieldsets)
     96        self.assertEqual(ua.get_fieldsets(request, user), change_fieldsets)
     97
     98        user.is_deleted = True
     99        self.assertEqual(ua.get_fieldsets(request, user), change_fieldsets)
     100
    70101    def test_get_fieldsets(self):
    71102        # get_fieldsets() is called when figuring out form fields (#18681).
    72103        class BandAdmin(ModelAdmin):
Back to Top