Friday, June 26, 2015

Re: Cannot overwrite 'get_fieldsets' in custom UserAdmin

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


Am Freitag, 26. Juni 2015 02:16:24 UTC+2 schrieb Henri:
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.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/be0b1c0b-bc31-4ddb-ba5f-42b428a925e6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment