Cannot overwrite 'get_fieldsets' in custom UserAdmin

376 views
Skip to first unread message

he...@art.bieszczady.pl

unread,
Jun 25, 2015, 8:16:24 PM6/25/15
to django...@googlegroups.com
I'm using a custom user model and a custom UserAdmin.
I try do overwrite the 'get_fieldsets' method to change some fields and hide some others, bud django admin still uses the  'get_fieldsets' method from django.contrib.auth.admin.UserAdmin.

My CustomUserAdmin Class:

from django.contrib.auth.admin import UserAdmin
from users.models import User  # custom user model


class CustomUserAdmin(UserAdmin):

    add_fieldsets
= (
       
(None, {
           
'classes': ('wide',),
           
'fields': ('email', 'username', 'password1', 'password2')
       
}),
   
)

   
# The forms to add and change user instances
    form
= UserChangeForm
    add_form
= UserCreationForm

   
# The fields to be used in displaying the User model.
   
# These override the definitions on the base UserAdmin
   
# that reference specific fields on auth.User.
    list_display
= ('email', 'username', 'first_name', 'last_name', 'is_active', 'is_staff',
                   
'is_superuser')
    list_filter
= ('is_active', 'is_staff', 'is_superuser')
    search_fields
= ('email', 'first_name', 'last_name', 'username')
    ordering
= ('email',)
    filter_horizontal
= ('groups', 'user_permissions',)


   
def get_fieldsets(self, request, obj=None):
       
if not obj:
           
return self.add_fieldsets

       
if request.user.is_superuser and request.user.pk != obj.pk:
            perm_fields
= ('is_active', 'is_staff', 'is_superuser',
                           
'groups', 'user_permissions')
       
else:
            perm_fields
= ('is_active', 'is_staff', 'groups')

       
return [(None, {'fields': ('email', 'password')}),
               
(_('Personal info'), {'fields': ('first_name', 'last_name', 'username', 'biography')}),
               
(_('Permissions'), {'fields': perm_fields}),
               
(_('Important dates'), {'fields': ('last_login', 'date_joined')})]


admin
.site.register(User, CustomUserAdmin)




Some body have an idea, whats going on?
I'm working on this for days.
Please help.

Henri

unread,
Jun 26, 2015, 6:19:49 PM6/26/15
to django...@googlegroups.com
Some more details:

I'm using Django 1.6.
When I debug the site, I see that users.admin.CustomUserAdmin is scanned, when starting the App.
But when loading the UserChangeForm the 'get_fieldsets' method from django.contrib.auth.admin.UserAdmin is used.

The custom UserChangeForm looks kike this:

class UserChangeForm(forms.ModelForm):
 
"""A form for updating users. Includes all the fields on
 the user, but replaces the password field with admin's
 password hash display field.
 """

 password
= ReadOnlyPasswordHashField(label=_("Password"),
 help_text
=_("Raw passwords are not stored, so there is no way to see "
 
"this user's password, but you can change the password "
 
"using <a href=\"password/\">this form</a>."))
 username
= forms.CharField(label=_('nickname'), max_length=64,
 help_text
=_('How do people call you?'))
 email
= forms.EmailField(label=_('email address'), max_length=254)

 
class Meta:
 model
= User
 fields
= '__all__'

 
def __init__(self, *args, **kwargs):
 
super(UserChangeForm, self).__init__(*args, **kwargs)
 f
= self.fields.get('user_permissions', None)
 
if f is not None:
 f
.queryset = f.queryset.select_related('content_type')

 
def clean_password(self):
 
# Regardless of what the user provides, return the initial value.
 
# This is done here, rather than on the field, because the
 
# field does not have access to the initial value
 
return self.initial["password"]

Somebody has an idea?

Regards,
Henri

Lagovas Lagovas

unread,
Jun 28, 2015, 3:48:35 AM6/28/15
to django...@googlegroups.com
first of all you need unregister default django models and then register yours AdminModel like:
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)

пятница, 26 июня 2015 г., 3:16:24 UTC+3 пользователь Henri написал:

Henri

unread,
Jun 28, 2015, 5:58:08 AM6/28/15
to django...@googlegroups.com
As I use a custom user model the default django user model is not registered. When I try to unregister it I get an error.

See example in the django docs:
https://docs.djangoproject.com/en/1.6/topics/auth/customizing/#a-full-example.

Regards, Henri

Felippe Raposo

unread,
Jun 29, 2015, 10:15:16 AM6/29/15
to django...@googlegroups.com
Henri, can you set a "pdb.set_trace" or print something  at the beginning of your "get_fieldsets" to see if it is being called or not?


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b169e3a8-29a5-4d33-b543-06b8d53597f0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Henri

unread,
Jun 29, 2015, 7:36:41 PM6/29/15
to django...@googlegroups.com
Hi Lagovas,

the trace and error message when unregistering the default Django user with your code:

  File "/home/pacha/web/rcl-network.local/private/django/rcl-network/venv/local/lib/python2.7/site-packages/mezzanine/boot/__init__.py", line 78, in autodiscover
    admin_site.lazy_registration()
  File "/home/pacha/web/rcl-network.local/private/django/rcl-network/venv/local/lib/python2.7/site-packages/mezzanine/boot/lazy_admin.py", line 33, in lazy_registration
    getattr(AdminSite, name)(self, *deferred_args, **deferred_kwargs)
  File "/home/pacha/web/rcl-network.local/private/django/rcl-network/venv/local/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 107, in unregister
    raise NotRegistered('The model %s is not registered' % model.__name__)
django.contrib.admin.sites.NotRegistered: The model User is not registered

Henri

unread,
Jun 29, 2015, 7:44:23 PM6/29/15
to django...@googlegroups.com
Felippe,

No, it seems, that my"get_fieldsets"  isn't called.

Also when debugging by stepping through the code, as described in my second post, I see when loading the UserChangeForm the 'get_fieldsets' method from django.contrib.auth.admin.UserAdmin is used instead of my custom one.

Regards, Henri
Reply all
Reply to author
Forward
0 new messages