为了账号安全,请及时绑定邮箱和手机立即绑定

/accounts/signup/ Manager 处的 AttributeError 不可用;

/accounts/signup/ Manager 处的 AttributeError 不可用;

大话西游666 2022-12-20 14:50:51
我有一个自定义注册表单SignupForm,用于检查email自定义用户对象是否存在CustomUser,如果存在则引发ValidationError。但是当我尝试提出错误时,我得到了AttributeError at /accounts/signup/  Manager isn't available; 'auth.User' has been swapped for 'accounts.CustomUser'。这是我的代码。forms.pyfrom django.contrib.auth.forms import UserCreationFormfrom django import formsfrom django.core.exceptions import ValidationErrorfrom django.contrib.auth import get_user_modelclass SignupForm(UserCreationForm):    def __init__(self, *args, **kwargs):        super(UserCreationForm, self).__init__(*args, **kwargs)    email = forms.CharField(        widget=forms.EmailInput(        attrs={            'class': 'input',            'placeholder': 'bearclaw@example.com'        }    ))    ...    # other fields (username and password)    ...    def clean(self):       User = get_user_model()       email = self.cleaned_data.get('email')       if User.objects.filter(email=email).exists():            raise ValidationError("An account with this email exists.")       return self.cleaned_dataviews.pyfrom django.urls import reverse_lazyfrom django.views.generic import CreateViewfrom .forms import SignupFormfrom .models import CustomUser...# other views and imports...class CustomSignup(CreateView):    form_class = SignupForm    success_url = reverse_lazy('login')    template_name = 'registration/signup.html'models.pyfrom django.db import modelsfrom django.contrib.auth.models import AbstractUserclass CustomUser(AbstractUser):    email = models.EmailField(unique=True)    def __str__(self):        return self.usernamesettings.pyAUTH_USER_MODEL = "accounts.CustomUser"我错过了什么?
查看完整描述

1 回答

?
胡子哥哥

TA贡献1825条经验 获得超6个赞

基本上你缺少 ModelForm Meta


试试这个


class SignupForm(UserCreationForm):


    def __init__(self, *args, **kwargs):

        super(UserCreationForm, self).__init__(*args, **kwargs)


    email = forms.CharField(

        widget=forms.EmailInput(

        attrs={

            'class': 'input',

            'placeholder': 'bearclaw@example.com'

        }

    ))


    ...

    # other fields (username and password)

    ...


    def clean(self):

       User = get_user_model()

       email = self.cleaned_data.get('email')

       if User.objects.filter(email=email).exists():

            raise ValidationError("An account with this email exists.")

       return self.cleaned_data


   class Meta:

       model = get_user_model()

       fields = ('username', 'email')


查看完整回答
反对 回复 2022-12-20
  • 1 回答
  • 0 关注
  • 76 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信