Now working correctly the create and modify user on the admin
parent
b7a6d89530
commit
91aeb00297
|
@ -1,8 +1,11 @@
|
|||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from django.contrib import admin
|
||||
from django.contrib.auth.models import Group
|
||||
from django.contrib.auth.admin import UserAdmin
|
||||
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
|
||||
|
||||
from greenmine.base.models import Role, User
|
||||
from greenmine.base.forms import UserChangeForm, UserCreationForm
|
||||
|
||||
admin.site.unregister(Group)
|
||||
|
||||
|
@ -20,4 +23,17 @@ class RoleAdmin(admin.ModelAdmin):
|
|||
db_field, request=request, **kwargs)
|
||||
|
||||
admin.site.register(Role, RoleAdmin)
|
||||
|
||||
class UserAdmin(DjangoUserAdmin):
|
||||
fieldsets = (
|
||||
(None, {'fields': ('username', 'password')}),
|
||||
(_('Personal info'), {'fields': ('first_name', 'last_name', 'email', 'description', 'photo')}),
|
||||
(_('Extra info'), {'fields': ('color', 'default_language', 'default_timezone', 'token', 'colorize_tags')}),
|
||||
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
|
||||
'groups', 'user_permissions')}),
|
||||
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
|
||||
)
|
||||
form = UserChangeForm
|
||||
add_form = UserCreationForm
|
||||
|
||||
admin.site.register(User, UserAdmin)
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
from django.contrib.auth.forms import UserCreationForm as DjangoUserCreationForm, UserChangeForm as DjangoUserChangeForm
|
||||
from greenmine.base.models import User
|
||||
|
||||
class UserCreationForm(DjangoUserCreationForm):
|
||||
def clean_username(self):
|
||||
# Since User.username is unique, this check is redundant,
|
||||
# but it sets a nicer error message than the ORM. See #13147.
|
||||
username = self.cleaned_data["username"]
|
||||
try:
|
||||
User._default_manager.get(username=username)
|
||||
except User.DoesNotExist:
|
||||
return username
|
||||
raise forms.ValidationError(self.error_messages['duplicate_username'])
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ('username',)
|
||||
|
||||
class UserChangeForm(DjangoUserChangeForm):
|
||||
class Meta:
|
||||
model = User
|
||||
|
Loading…
Reference in New Issue