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)
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"]
first of all you need unregister default django models and then register yours AdminModel like:
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
--
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.