【问题标题】:AttributeError at /accounts/signup/ Manager isn't available; 'auth.User' has been swapped for 'accounts.CustomUser'/accounts/signup/ Manager 的 AttributeError 不可用; 'auth.User' 已替换为 'accounts.CustomUser'
【发布时间】:2020-09-20 20:48:05
【问题描述】:

我有一个自定义注册表单 SignupForm,它检查自定义用户对象 CustomUser 的现有 email 并引发 ValidationError(如果存在)。但是当我尝试提出错误时,我得到了AttributeError at /accounts/signup/ Manager isn't available; 'auth.User' has been swapped for 'accounts.CustomUser'

这是我的代码。

forms.py

from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.core.exceptions import ValidationError

from django.contrib.auth import get_user_model


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

views.py

from django.urls import reverse_lazy
from django.views.generic import CreateView
from .forms import SignupForm
from .models import CustomUser

...
# other views and imports
...

class CustomSignup(CreateView):
    form_class = SignupForm
    success_url = reverse_lazy('login')
    template_name = 'registration/signup.html'

models.py

from django.db import models
from django.contrib.auth.models import AbstractUser


class CustomUser(AbstractUser):
    email = models.EmailField(unique=True)

    def __str__(self):
        return self.username

settings.py

AUTH_USER_MODEL = "accounts.CustomUser"

我错过了什么?

【问题讨论】:

    标签: python django django-models django-forms django-views


    【解决方案1】:

    基本上你缺少 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')
    

    【讨论】:

    • 谢谢。还发现了一个有用的comment。似乎model 被硬编码在contrib.auth.forms.UserCreationFormMeta 中。
    猜你喜欢
    • 2018-03-19
    • 1970-01-01
    • 2021-12-24
    • 2019-07-20
    • 2018-07-27
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多