Monday, June 29, 2015

Re: Cannot overwrite 'get_fieldsets' in custom UserAdmin

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


Am Sonntag, 28. Juni 2015 09:48:35 UTC+2 schrieb Lagovas Lagovas:
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 написал:
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/c2f195b5-2151-4bbd-ab39-eb978ae5a229%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment