Я создал пользовательскую модель пользователя, используя Django встроенный пользователь, и добавил свои собственные поля. Когда я пытаюсь запустить код, я получаю сообщение об ошибке. Я искал везде, и я не получаю ответ на эту ошибку.
Я запустил код с пустым admin.py, он отлично работал для входа в систему / выхода из системы и регистрации, но ничего, что я показал в Django admin.
Затем после некоторого исследования я пришел с кодом, упомянутым ниже, но все же я получаю ошибку!
models.py
from django.db import models
from django.contrib.auth.models import BaseUserManager,AbstractBaseUser
class MyAccountManager(BaseUserManager):
def create_user(self, email, username, address,phone_number,password=None):
if not email:
raise ValueError('Users must have an email address')
if not username:
raise ValueError('Users must have a username')
user = self.model(
email=self.normalize_email(email),
username=username,
address=address,
phone_number=phone_number,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, username, address,phone_number,password):
user = self.create_user(
email=self.normalize_email(email),
password=password,
username=username,
address=address,
phone_number=phone_number,
)
user.is_admin = True
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
class CustomUser(AbstractBaseUser):
email = models.EmailField(verbose_name="email",max_length=60,unique=True)
username= models.CharField(max_length=30,unique=True)
phone_number= models.IntegerField()
address=models.CharField(max_length=300)
is_active=models.BooleanField(default=True)
is_admin=models.BooleanField(default=False)
is_staff=models.BooleanField(default=False)
is_superuser=models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username','address','phone_number']
objects = MyAccountManager()
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self, app_label):
return True
forms.py
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm,UserChangeForm
from django import forms
from . models import CustomUser
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = UserChangeForm.Meta.fields
class UserCreateform(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = CustomUser
fields = UserCreationForm.Meta.fields+('address','phone_number')
admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser
from .forms import UserCreateform,CustomUserChangeForm
class UserAd(UserAdmin):
add_form = UserCreateform
form = CustomUserChangeForm
model = CustomUser
list_display = ("email","username","address","phone_number","is_staff")
list_filter = ("is_staff","is_superuser","is_active","groups")
search_fields = ("username","email")
ordering=("username")
filter_horizontal=("groups","user_permissions",)
fieldsets = (
( None, {"fields": ("username","password")}),
( ("Personal Info"),{"fields":("email","address","phone_number")}),
( ("Permissions"),{"fields":("is_admin","is_active","is_staff","is_superuser","groups","user_permissions")}),
( ("Important Dates"),{"fields":("last_login","date_joined")}),
)
add_fieldsets = (
(
None,
{
"classes":("wide",),
"fields":("username","password1","password2"),
},
),
)
admin.site.register(CustomUser, UserAd)
Сообщение об ошибке!
<class 'account.admin.UserAd'>: (admin.E019) The value of 'filter_horizontal[0]' refers to 'groups', which is not an attribute of 'account.CustomUser'.
<class 'account.admin.UserAd'>: (admin.E019) The value of 'filter_horizontal[1]' refers to 'user_permissions', which is not an attribute of 'account.CustomUser'.
<class 'account.admin.UserAd'>: (admin.E031) The value of 'ordering' must be a list or tuple.
<class 'account.admin.UserAd'>: (admin.E116) The value of 'list_filter[3]' refers to 'groups', which does not refer to a Field.