【发布时间】:2021-08-25 17:14:35
【问题描述】:
我是 django 的新手,我正在尝试用一些额外的参数来扩展 Django 用户模型。我尝试了以下解决方案并尝试了不同的解决方法,但没有成功。任何帮助将不胜感激。
这里是代码文件。
管理员 -> init__.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from usermanagement.models import UserProfile
scrumboard_models = [models.Board, models.Setting, models.CheckList, models.CheckListItem,
models.Comments, models.Subscribers, models.Lists,
models.Label, models.Card, models.Attachment, models.Team]
admin.site.register(scrumboard_models)
class UserProfileAdmin(UserAdmin):
list_display = (
'id', 'first_name', 'username', 'email', 'is_active', 'dob'
)
admin.site.register(UserProfile, UserProfileAdmin)
Settings.py """ 应用项目的 Django 设置。
Generated by 'django-admin startproject' using Django 3.1.7.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""
import os
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
import datetime
JWT_AUTH = {
'JWT_ALLOW_REFRESH': True,
'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=3600),
}
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'v!t5abxm^t@tyj=z5gp!*_%qy=$q-d6g!4zx(!r3_nn)7h^ee7'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.postgres',
'scrumboard',
'admin_app',
'whitenoise.runserver_nostatic',
'corsheaders',
'usermanagement',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'corsheaders.middleware.CorsMiddleware',
]
ROOT_URLCONF = 'app.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'app.wsgi.application'
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': [
'django_filters.rest_framework.DjangoFilterBackend'
],
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_simplejwt.authentication.JWTAuthentication',
],
}
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
"NAME": os.environ.get('DB_NAME', 'scrumboard'),
"USER": os.environ.get('USER', 'postgres'),
"PASSWORD": os.environ.get('PASSWORD', '112233'),
"HOST": os.environ.get('HOST', 'localhost'),
"PORT": os.environ.get('DB_PORT', 5432),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = 'abc@gmail.com'
EMAIL_HOST_PASSWORD = '1234567890'
EMAIL_USE_SSL=False
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# ======================================================================================================================
# CORS SETTINGS
# ======================================================================================================================
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
AUTH_USER_MODEL = 'usermanagement.UserProfile'
usermanagement.models.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
# Create your models here.
class CustomAccountManager(BaseUserManager):
def create_superuser(self, email, username, first_name, password, **other_fields):
other_fields.setdefault('is_staff', True)
other_fields.setdefault('is_superuser', True)
other_fields.setdefault('is_active', True)
if other_fields.get('dob') is not True:
dob = None
if other_fields.get('is_staff') is not True:
raise ValueError(
'Superuser must be assigned to is_staff=True.')
if other_fields.get('is_superuser') is not True:
raise ValueError(
'Superuser must be assigned to is_superuser=True.')
return self.create_user(email, username, first_name, password, **other_fields)
def create_user(self, email, username, first_name, password, **other_fields):
if not email:
raise ValueError(_('You must provide an email address'))
email = self.normalize_email(email)
user = self.model(email=email, username=username,
first_name=first_name, password=password, **other_fields)
user.set_password(password)
user.save(using=self._db)
return user
class UserProfile(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), unique=True)
username = models.CharField(max_length=150, unique=True)
first_name = models.CharField(max_length=150, blank=True)
last_name = models.CharField(max_length=150, blank=True)
date_joined = models.DateTimeField(default=timezone.now)
about = models.TextField(_(
'about'), max_length=500, blank=True)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=False)
phone_number = models.CharField(max_length=20)
dob = models.DateField(blank=True, null=True)
gender = models.CharField(max_length=15)
objects = CustomAccountManager()
USERNAME_FIELD = 'email'
class Meta:
abstract = False
def __str__(self):
return self.username
[Project Structure][1]
[1]: https://i.stack.imgur.com/06Wkp.png
参数 dob , gender 已添加到数据库中,但未显示在 django 管理门户中。
【问题讨论】:
-
你能把错误贴出来
-
没有错误。但是在创建新用户时,字段扩展字段没有显示在 django 管理门户中
标签: python django django-rest-framework django-admin