经理不在; 'auth.User' 已换成 'users.CustomUser'
Manager isn't available; 'auth.User' has been swapped for 'users.CustomUser'
我是 Django 的新手,我正在尝试为我的网站实施自定义用户身份验证。当我尝试为注册视图创建新用户时,出现错误“经理不可用; 'auth.User' 已替换为 'users.CustomUser'。从管理页面创建新用户工作正常。
我用谷歌搜索了这个错误,但是 none 建议的解决方案对我有用。
查看下面的代码。我创建的用于管理用户的 django 应用程序是 "users"。
models.py:
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
birth_date = models.DateField(null=True, blank=True)
forms.py:
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
class CustomUserCreationForm(UserCreationForm):
first_name = forms.CharField(max_length=30, help_text='Required`.')
last_name = forms.CharField(max_length=30, help_text='Required.')
email = forms.EmailField(max_length=254, help_text='Required. Please input a valid email address.')
birth_date = forms.DateField()
class meta(UserCreationForm.Meta):
model = CustomUser
fields = UserCreationForm.Meta.fields + ('first_name', 'last_name', 'email', 'birth_date',)
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = UserChangeForm.Meta.fields
views.py:
from .forms import CustomUserCreationForm
from django.urls import reverse_lazy
from django.views.generic import CreateView
class SignUpView(CreateView):
form_class = CustomUserCreationForm
success_url = reverse_lazy('login')
template_name = 'signup.html'
admin.py:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import CustomUser
class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
form = CustomUserChangeForm
model = CustomUser
list_display = ['username', 'email', 'first_name', 'last_name', 'is_staff',]
admin.site.register(CustomUser, CustomUserAdmin)
settings.py:
INSTALLED_APPS = [
# Local
'users.apps.UsersConfig',
'pages.apps.PagesConfig',
'casualties.apps.CasualtiesConfig',
# 3rd Party
'import_export',
'django_tables2',
'django_filters',
'bootstrap4',
# Django
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
...
AUTH_USER_MODEL = 'users.CustomUser'
在此先感谢您的帮助!!!
经过更多尝试,已解决。这是工作代码:
models.py:
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
pass
birth_date = models.DateField(null=True, blank=True)
def __str__(self):
return self.username
forms.py:
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = CustomUser
fields = ('username', 'first_name', 'last_name', 'email')
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = ('username', 'first_name', 'last_name', 'email')
def __init__(self, *args, **kwargs):
super(CustomUserChangeForm, self).__init__(*args, **kwargs)
del self.fields['password']
我是 Django 的新手,我正在尝试为我的网站实施自定义用户身份验证。当我尝试为注册视图创建新用户时,出现错误“经理不可用; 'auth.User' 已替换为 'users.CustomUser'。从管理页面创建新用户工作正常。
我用谷歌搜索了这个错误,但是 none 建议的解决方案对我有用。
查看下面的代码。我创建的用于管理用户的 django 应用程序是 "users"。
models.py:
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
birth_date = models.DateField(null=True, blank=True)
forms.py:
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
class CustomUserCreationForm(UserCreationForm):
first_name = forms.CharField(max_length=30, help_text='Required`.')
last_name = forms.CharField(max_length=30, help_text='Required.')
email = forms.EmailField(max_length=254, help_text='Required. Please input a valid email address.')
birth_date = forms.DateField()
class meta(UserCreationForm.Meta):
model = CustomUser
fields = UserCreationForm.Meta.fields + ('first_name', 'last_name', 'email', 'birth_date',)
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = UserChangeForm.Meta.fields
views.py:
from .forms import CustomUserCreationForm
from django.urls import reverse_lazy
from django.views.generic import CreateView
class SignUpView(CreateView):
form_class = CustomUserCreationForm
success_url = reverse_lazy('login')
template_name = 'signup.html'
admin.py:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import CustomUser
class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
form = CustomUserChangeForm
model = CustomUser
list_display = ['username', 'email', 'first_name', 'last_name', 'is_staff',]
admin.site.register(CustomUser, CustomUserAdmin)
settings.py:
INSTALLED_APPS = [
# Local
'users.apps.UsersConfig',
'pages.apps.PagesConfig',
'casualties.apps.CasualtiesConfig',
# 3rd Party
'import_export',
'django_tables2',
'django_filters',
'bootstrap4',
# Django
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
...
AUTH_USER_MODEL = 'users.CustomUser'
在此先感谢您的帮助!!!
经过更多尝试,已解决。这是工作代码:
models.py:
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
pass
birth_date = models.DateField(null=True, blank=True)
def __str__(self):
return self.username
forms.py:
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = CustomUser
fields = ('username', 'first_name', 'last_name', 'email')
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = ('username', 'first_name', 'last_name', 'email')
def __init__(self, *args, **kwargs):
super(CustomUserChangeForm, self).__init__(*args, **kwargs)
del self.fields['password']