[英]Django create CustomUser model
Folks,伙计们,
I'm trying to create my own User model by extending the AbstractBaseUser model provided by Django.我正在尝试通过扩展 Django 提供的 AbstractBaseUser 模型来创建我自己的 User 模型。
However, I keep on getting the following error when migrating: ValueError: The field admin.LogEntry.user was declared with a lazy reference to '<app_name>.user', but app '<app_name>' doesn't provide model 'user'.
但是,我在迁移时不断收到以下错误: ValueError: The field admin.LogEntry.user was declared with a lazy reference to '<app_name>.user', but app '<app_name>' doesn't provide model 'user'.
What I have done so far is the following:到目前为止,我所做的如下:
In app/models.py
added CustomUser class together with CustomUserManager with relevant fields and all.在app/models.py
添加了 CustomUser 类以及带有相关字段和所有内容的 CustomUserManager。
In app/admin.py
I've added this:在app/admin.py
我添加了这个:
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from <app_name>.models import CustomUser
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = get_user_model()
fields = ('email',)
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = get_user_model()
fields = ('email',)
def clean_username(self):
username = self.cleaned_data["username"]
try:
get_user_model().objects.get(username=username)
except get_user_model().DoesNotExist:
return username
raise forms.ValidationError(self.error_messages['duplicate_username'])
class CustomUserAdmin(UserAdmin):
form = CustomUserChangeForm
add_form = CustomUserCreationForm
fieldsets = (
(None, {'fields': [('username', 'password'),]}),
(('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
(('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
'groups', 'user_permissions')}),
(('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
admin.site.register(CustomUser, CustomUserAdmin)
Apart from that there is this added to seetings.py
除此之外还有这个添加到seetings.py
AUTH_USER_MODEL = '<app_name>.CustomUser'
Everything that I found so far suggests that the above given code should make this work, but it doesn't.到目前为止,我发现的所有内容都表明上面给出的代码应该可以完成这项工作,但事实并非如此。 I spent like 4-5 hours already and I still can't get my head around it.我已经花了大约 4-5 个小时,但我仍然无法理解它。 Someone please help有人请帮忙
Create your User model like so:像这样创建您的用户模型:
from django.core import validators
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin
from django.contrib.auth.models import UserManager
class User(AbstractBaseUser, PermissionsMixin):
username = models.CharField(_('username'), max_length=75, unique=True,
help_text=_('Required. 30 characters or fewer. Letters, numbers and '
'underscores characters'),
validators=[
validators.RegexValidator(re.compile('^[\w]+$'),
_('Enter a valid username.'), 'invalid')
])
first_name = models.CharField(_('first name'), max_length=254, blank=True)
last_name = models.CharField(_('last name'), max_length=30, blank=True)
email = models.EmailField(_('email address'), max_length = 254, unique = True, null = True)
is_staff = models.BooleanField(_('staff status'), default=False,
help_text=_('Designates whether the user can log into this admin '
'site.'))
is_active = models.BooleanField(_('active'), default=True,
help_text=_('Designates whether this user should be treated as '
'active. Unselect this instead of deleting accounts.'))
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
objects = UserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['first_name']
def get_full_name(self):
return self.name
def get_short_name(self):
return self.username
Don't forget to add your custom fields!不要忘记添加您的自定义字段!
Use a custom admin like so:像这样使用自定义管理员:
@admin.register(models.User)
class CustomUserAdmin(UserAdmin):
list_display = ('name', 'username', 'is_staff', )
list_filter = ('is_staff', )
search_fields = ('first_name', 'last_name', 'username', )
readonly_fields = ('date_joined', 'last_login', )
fieldsets = (
(None, {
'fields': ('username', 'password')
}),
("Personal Information", {
'fields': ('first_name', 'last_name', 'email')
}),
("Permissions", {
'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')
}),
("Important Dates", {
'fields': ('last_login', 'date_joined')
}),
)
Then finally add it to your settings:然后最后将其添加到您的设置中:
AUTH_USER_MODEL = '<app_name>.User'
A working demonstration can be found in my GitHub repository .可以在我的GitHub 存储库中找到工作演示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.