这是我做到的方式:
注意:这应该在你创建新项目时完成。
向用户模型添加字段:
models.py:
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
gender = models.BooleanField(default=True) # True for male and False for female
# you can add more fields here.
重写默认用户模型:
settings.py:
# the example_app is an app in which models.py is defined
AUTH_USER_MODEL = 'example_app.User'
在管理页面上显示模型:-from django.contrib import admin
from .models import User
admin.site.register(User)
class UserProfile(models.Model):
user = models.OneToOneField(User)
phone = models.CharField(max_length=256, blank=True, null=True)
gender = models.CharField(
max_length=1, choices=(('m', _('Male')), ('f', _('Female'))),
blank=True, null=True)
您可以在用户模型或用户资料模型中使用Django管理界面,并根据需要在管理界面中显示字段。
您可以使用用户模型和OneToOneField()来扩展额外字段。*您还可以查看我的答案,了解如何使用AbstractUser或AbstractBaseUser和PermissionsMixin设置email
和password
身份验证。
首先,运行以下命令创建account
应用程序:
python manage.py startapp account
# "settings.py"
INSTALLED_APPS = [
...
"account", # Here
]
account/models.py
中创建UserProfile
模型,如下所示。*user
字段使用OneToOneField()
扩展了User
模型:# "account/models.py"
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(
User,
verbose_name=_("user"),
on_delete=models.CASCADE
)
age = models.PositiveSmallIntegerField(_("age"))
gender = models.CharField(_("gender"), max_length=20)
married = models.BooleanField(_("married"))
# "account/admin.py"
from django.contrib import admin
from .models import UserProfile
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
admin.site.unregister(User) # Necessary
class UserProfileInline(admin.TabularInline):
model = UserProfile
@admin.register(User)
class UserAdmin(BaseUserAdmin):
inlines = (UserProfileInline,)
python manage.py makemigrations && python manage.py migrate
python manage.py runserver 0.0.0.0:8000
User
模型,添加额外的字段:
#managers.py Create new file.
from django.contrib.auth.base_user import BaseUserManager
from django.utils.translation import ugettext_lazy as _
class CustomUserManager(BaseUserManager):
def create_user(self, email, password, **extra_fields):
if not email:
raise ValueError(_('The Email must be set'))
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save()
return user
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', True)
if extra_fields.get('is_staff') is not True:
raise ValueError(_('Superuser must have is_staff=True.'))
if extra_fields.get('is_superuser') is not True:
raise ValueError(_('Superuser must have is_superuser=True.'))
return self.create_user(email, password, **extra_fields)
#models.py Create your models here.
from django.db import models
from django.contrib.auth.models import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin
from django.utils.translation import gettext_lazy as _
from .managers import CustomUserManager
class User(AbstractBaseUser,PermissionsMixin):
first_name =models.CharField(max_length=250)
email = models.EmailField(_('email address'), unique=True)
mobile =models.CharField(max_length=10)
status = models.BooleanField(default=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
object =CustomUserManager()
# add more your fields
#admin.py
from django.contrib import admin
from .models import User
@admin.register(User)
class UserAdmin(admin.ModelAdmin):
list_display = ('email','mobile','password')
#setting.py
AUTH_USER_MODEL = 'users.User'
# run command
python manage.py makemigrations
python manage.py migrate