Django 自定义模型、电子邮件字段在由管理员创建用户时禁用填充

如何解决Django 自定义模型、电子邮件字段在由管理员创建用户时禁用填充

  1. 我将默认用户模型更改为我的自定义用户模型并使用电子邮件登录。
  2. 登录管理员并尝试创建新用户后,无法填写电子邮件字段。喜欢下面的截图。
  3. 我在解决其他问题时删除了 db.sqlite3。但稍后已经重新构建。
  4. python manage.py createsuperuser 仍然有效。我可以通过终端创建超级用户。

enter image description here

这里是代码


| ./accounts/admin.py |

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
# Register your models here.

from .models import Account

class AccountAdmin(UserAdmin):

    list_display = ('email','date_joined','last_login','is_admin')
    search_fields = ('email',)
    readonly_fields = ('email','last_login')

    ordering = ('email',) # solution for E033

    filter_horizontal = ()
    list_filter = ()

    fieldsets = (
        (None,{'fields': ('email','password')}),)

    add_fieldsets = (
        (None,{
            'classes': ('wide',),'fields': ('email','password1','password2'),}),)
admin.site.register(Account,AccountAdmin)

| ./accounts/backends.py |

from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend


class EmailAuthBackend(ModelBackend):

    def authenticate(self,request,email=None,password=None,**kwargs):
        UserModel = get_user_model()
        if email is None:
            email = kwargs.get(UserModel.USERNAME_FIELD)
        if email is None or password is None:
            return
        try:
            user = UserModel._default_manager.get_by_natural_key(email)
        except UserModel.DoesNotExist:
            # Run the default password hasher once to reduce the timing
            # difference between an existing and a nonexistent user (#20760).
            UserModel().set_password(password)
        else:
            if user.check_password(password) and self.user_can_authenticate(user):
                return user

| ./accounts/models.py |

from django.db import models
from django.contrib.auth.models import AbstractBaseUser,BaseUserManager

class AccountManager(BaseUserManager):

    def create_user(self,email,password=None):

        if not email:
            raise ValueError("User must register with email.")

        user = self.model(
            email = self.normalize_email(email),)

        user.set_password(password)
        user.save(using=self.db)
        return user

    def create_superuser(self,password):

        user = self.create_user(
            email = self.normalize_email(email),password = password,)

        user.is_staff = True
        user.is_admin = True
        user.is_superuser = True
        user.save(using=self.db)
        return user


class Account(AbstractBaseUser):

    email = models.EmailField(unique=True)
    date_joined = models.DateTimeField(auto_now_add=True)
    last_login = models.DateTimeField(auto_now=True)
    is_staff = models.BooleanField(default=False)
    is_admin = models.BooleanField(default=False)
    is_activate = models.BooleanField(default=True)
    is_superuser = models.BooleanField(default=False)

    objects = AccountManager()

    USERNAME_FIELD = 'email'

    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


| ./accounts/views.py |

from django.views.generic import TemplateView
from django.shortcuts import render,redirect
from django.contrib.auth import login,logout,authenticate

from .forms import RegisterationForm,LogInForm


class HomePageView(TemplateView):

    template_name = 'index.html'


def RegisterView(request):

    user = request.user
    if user.is_authenticated:
        return redirect('home')

    context = {}
    if request.POST:
        form = RegisterationForm(request.POST)
        if form.is_valid():
            user = form.save()
            login(request,user,backend='accounts.backends.EmailAuthBackend')
            return redirect('home')
    else:
        form = RegisterationForm()

    context['register_form'] = form
    return render(request,'accounts/register.html',context)

def LogInView(request):

    user = request.user
    if user.is_authenticated:
        return redirect('home')

    context = {}
    if request.POST:
        form = LogInForm(request.POST)
        if form.is_valid():
            email = request.POST['email']
            password = request.POST['password']
            user = authenticate(email=email,password=password)
            if user:
                login(request,backend='accounts.backends.EmailAuthBackend')
                return redirect('home')

    else:
        form = LogInForm()

    context['login_form'] = form
    return render(request,"accounts/login.html",context)


def LogOutView(request):
    logout(request)
    return redirect('home')

| ./accounts/forms.py |

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import authenticate

from .models import Account

class RegisterationForm(UserCreationForm):

    email = forms.EmailField(label='email',error_messages={
                                                              'invalid': 'Email Invaild','required':'Enter email please.'
                                                              })

    password1 = forms.EmailField(error_messages={'required':'Enter password please.'})
    password2 = forms.EmailField(error_messages={'required':'Enter password confirmation please.'})

    error_messages = {
        'password_mismatch': ('Two passwords did not match.'),}

    class Meta:

        model = Account
        fields = ('email','password2')

    def clean_email(self):

        email = self.cleaned_data['email']
        try:
            account = Account.objects.get(email=email)
        except Exception as e:
            return email
        raise forms.ValidationError(f'{email} has been registered.')


class LogInForm(forms.ModelForm):

    email = forms.EmailField(error_messages={'required':'Enter email.'})
    password = forms.CharField(error_messages={'required':'Enter password.'})

    class Meta:
        model = Account
        fields = ('email','password')

    def clean(self):
        if self.is_valid():
            email = self.cleaned_data['email']
            password = self.cleaned_data['password']
            if not authenticate(email=email,password=password):
                raise forms.ValidationError("Login Fail!")

我也使用自定义密码验证器。

| ./accounts/validators.py |

from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _

class CustomizePasswordValidator():

    def __init__(self,min_length=8):
        self.min_length = min_length

    def validate(self,password,user=None):

        special_characters = "[~\!@#\$%\^&\*\(\)_\+{}\":;'\[\]]"

        if ( len(password) < 8 ) \
        or ( password.isalpha() ) \
        or ( password.isnumeric() ) \
        or ( password in special_characters) :
            raise ValidationError(_('Please enter more than 8 nums and alphabet combination.'))

    def get_help_text(self):
        return ""

| ./confif/settings.py |

import os

# Build paths inside the project like this: os.path.join(BASE_DIR,...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!


# 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','accounts',]

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',]

ROOT_URLCONF = 'config.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates','DIRS': [ os.path.join(BASE_DIR,'templates').replace('\\','/') ],'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 = 'config.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3','NAME': os.path.join(BASE_DIR,'db.sqlite3'),}
}


# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators

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',{
        'NAME': 'accounts.validators.CustomizePasswordValidator',]


# Internationalization
# https://docs.djangoproject.com/en/2.0/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/2.0/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_DIRS = [
                    os.path.join(BASE_DIR,"static"),]


AUTH_USER_MODEL = 'accounts.Account'
AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.AllowAllUsersModelBackend','accounts.backends.EmailAuthBackend',)

有没有人遇到过类似的情况?

解决方法

您的管理类 AccountAdmin 继承自 UserAdmin。如果您检查它的实现,它会设置 add_form = UserCreationForm,它的 Meta 类中有以下 line [Github code]fields = ("username",)

由于您已经创建了一个继承自 UserCreationForm 的自定义表单,因此您应该在您的管理类中进行设置:

from .forms import RegisterationForm


class AccountAdmin(UserAdmin):
    ...
    #              Remove ↓
    readonly_fields = ('email', 'date_joined','last_login')
    ...
    add_form = RegisterationForm

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res