我想使用abstractuser
. 我不想要任何特别的东西,只需要稍后在需要时添加自定义字段的能力。
我从文档中了解到我需要:
- 放
AUTH_USER_MODEL
- 通过子类化定义自定义用户模型
AbstractUser
- 通过子类化定义自定义模型管理器
UserManager
- 子类
UserCreationForm
和UserChangeForm
这是我当前的代码:
我的settings.py
:
AUTH_USER_MODEL = 'users.CustomUser'
我的models.py
:
from django.contrib.auth.models import AbstractUser, UserManager
class CustomUserManager(UserManager):
pass
class CustomUser(AbstractUser):
objects = CustomUserManager()
我的forms.py
:
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
class CustomUserCreationForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = CustomUser
fields = UserCreationForm.Meta.fields
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = UserChangeForm.Meta.fields
我的admin.py
:
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import CustomUser
class CustomUserAdmin(UserAdmin):
model = CustomUser
add_form = CustomUserCreationForm
form = CustomUserChangeForm
admin.site.register(CustomUser, CustomUserAdmin)
我的views.py
:
from django.urls import reverse_lazy
from django.views import generic
from .forms import CustomUserCreationForm
class SignUp(generic.CreateView):
form_class = CustomUserCreationForm
success_url = reverse_lazy('login')
template_name = 'signup.html'
提交注册表单后,收到错误消息:
OperationalError at /users/signup/
no such table: users_customuser
我已经核对了我的数据库并完成了makemigrations
,然后migrate
从头开始。我的表格有问题,也许管理员也有问题。