有人会帮助我吗?我已经尝试了很多。我已阅读文档,但不了解如何在“管理页面的用户”中添加自定义用户字段?每当我makemigrations
时,都会出现以下错误。
不同的页面
#admin.py
from django.contrib.auth.admin import UserAdmin
admin.site.register(UserProfile, UserAdmin)
#models.py
from django.contrib.auth.models import AbstractUser
class UserProfile(AbstractUser):
Id_card_number = models.CharField(max_length=15)
#forms.py
class UserRegisterForm(UserCreationForm):
email = forms.EmailField(required=True)._unique = True
Id_card_number = forms.CharField(max_length=15, required=True)._unique = True
class Meta:
model = UserProfile
fields = ['username','email','password1','password2','Id_card_number']
每当我在settings.py
中使用AUTH_USER_MODEL = 'users.UserProfile'
时发生错误
错误:auth.User.groups :(字段E304)的反向访问器 “ User.groups”与“ UserProfile.groups”的反向访问器冲突。 提示:在“ User.groups”或“ UserProfile.groups”的定义中添加或更改related_name参数。 auth.User.user_permissions: (字段E304)“ User.user_permissions”冲突的反向访问器 与反向访问“ UserProfile.user_permissions”。 提示:在“ User.user_permissions”或“ UserProfile.user_permissions”的定义中添加或更改related_name参数。 users.UserProfile.groups:(fields.E304)的反向访问器 “ UserProfile.groups”与“ User.groups”的反向访问器冲突。 提示:在“ UserProfile.groups”或“ User.groups”的定义中添加或更改related_name参数。 users.UserProfile.user_permissions:(fields.E304)的反向访问器 'UserProfile.user_permissions'与反向访问器的冲突 'User.user_permissions'。 提示:在“ UserProfile.user_permissions”或“ User.user_permissions”的定义中添加或更改related_name参数。
每当我在settings.py中不使用AUTH_USER_MODEL = 'users.UserProfile'
时出错。
错误:users.Profile.user:(fields.E301)字段定义关系 使用模型“ auth.User”(已换出)。暗示: 将关系更新为指向“ settings.AUTH_USER_MODEL”。
答案 0 :(得分:0)
您是否尝试导入from django.contrib.auth.models import AbstractBaseUser
?
您的用户随后将继承自AbstractBaseUser:
from django.contrib.auth.models import AbstractBaseUser
class UserProfile(AbstracBasetUser):
Id_card_number = models.CharField(max_length=15)
答案 1 :(得分:0)
model.py
from django.contrib.auth.models import User
class User(AbstractUser):
password1 = models.CharField(max_length=250)
password2 = models.CharField(max_length=250)
class Meta:
db_table = 'caccounts_user'
verbose_name = 'User'
您可以在上述代码的帮助下改写。放置上面的代码,然后依次make migation
和migarte
答案 2 :(得分:0)
我是这样做的,并且工作得很好:
#models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
# add additional fields in here
def __str__(self):
return self.email
#forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
class CustomUserCreationForm(UserCreationForm):
class Meta(UserCreationForm):
model = CustomUser
fields = ('username', 'email')
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = ('username', 'email')
#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):
add_form = CustomUserCreationForm
form = CustomUserChangeForm
model = CustomUser
list_display = ['email', 'username',]
admin.site.register(CustomUser, CustomUserAdmin)
在设置中:
AUTH_USER_MODEL = 'users.CustomUser'