mirror of
https://github.com/cookiecutter/cookiecutter-django.git
synced 2025-08-05 12:40:17 +03:00
Updated users/admin.py with fix for issue #146 to allow user creation through the Django admin
This commit is contained in:
parent
57eeef37d0
commit
1c0f4a6c05
|
@ -1,13 +1,27 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from django.contrib import admin
|
||||
from django import forms
|
||||
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
|
||||
from django.contrib.auth.admin import UserAdmin as AuthUserAdmin
|
||||
|
||||
from .models import User
|
||||
|
||||
|
||||
class MyUserCreationForm(UserCreationForm):
|
||||
def clean_username(self):
|
||||
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(UserCreationForm.Meta):
|
||||
model = User
|
||||
|
||||
|
||||
class UserAdmin(AuthUserAdmin):
|
||||
create_form_class = UserCreationForm
|
||||
add_form = MyUserCreationForm
|
||||
update_form_class = UserChangeForm
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user