django 异常值:"CustomUser"对象不可调用|自定义用户模型

64jmpszr  于 2023-02-06  发布在  Go
关注(0)|答案(1)|浏览(107)

我正在尝试创建一个自定义用户模型。该模型在命令提示符下工作正常,我也可以登录到管理面板。我也可以访问登录页面。但当我尝试访问注册页面时,系统显示以下错误。
错误图像

models.py

from django.db import models
from django.contrib import auth
from django.urls import reverse
# Create your models here.

# for custom user
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, User
from .managers import CustomUserManager

class CustomUser(AbstractBaseUser, PermissionsMixin):
    '''Model representation for user'''
    user_type_choices = (
        ('ps','problem_solver'),
        ('pp','problem_provider')
        )

    account_type_choices = (
        ('o','Organization'),
        ('i','Individual')
        )  
    
    user_type = models.CharField(max_length=5, choices=user_type_choices, default='pp', verbose_name="Who you are? ")
    account_type = models.CharField(max_length=5, choices= account_type_choices, default='o', verbose_name="Account Type ")
    email = models.EmailField(max_length=50, unique=True, blank=False, verbose_name="Your Email ")
    is_active = models.BooleanField(default=True) # anyone who signs up for thsi application is by default an active user   
    is_admin = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False) # the person who has highest level of control over database

    # need to specify manager class for this user
    objects = CustomUserManager()

    # we are not placing password field here because the password field will always be required
    REQUIRED_FIELDS = ['user_type', 'account_type']

    USERNAME_FIELD = 'email'
    EMAIL_FIELD = 'email'

# class User(User, PermissionsMixin): # for username and password

#     def __str__(self):
#         return "@{}".format(self.username)

    # def get_absolute_url(self):
    #     return reverse("Solver_detail", kwargs={"pk": self.pk})

views.py

from django.shortcuts import render
from django.urls import reverse_lazy
from . import forms
from django.views.generic import CreateView
from .managers import CustomUserManager

# Create your views here.
class SignUpView(CreateView):
    form_class = forms.SignUpForm
    success_url = reverse_lazy('login')
    template_name = 'accounts/signup.html'

managers.py

from django.contrib.auth.models import BaseUserManager
# from django.contrib.auth.admin import UserAdmin as BaseUserAdmin

class CustomUserManager(BaseUserManager):
    def create_user(self, user_type, account_type, email, password):
        if not email:
            raise ValueError('User must provide valis email address')
        if not password:
            raise ValueError('User must provide password')
        
        user = self.model(
            user_type = user_type,
            account_type = account_type,
            email = self.normalize_email(email=email) # it normalizes the email for storage
        )

        user.set_password(raw_password = password) # it hashes password before setting it up into the database
        user.save(using = self._db)
        return user

    
    def create_superuser(self, user_type, account_type, email, password):
        user = self.create_user(
            user_type= user_type,
            account_type= account_type,
            email=email, 
            password= password
        )
        user.is_admin = True
        user.is_staff = True
        user.is_superuser = True
        user.save(using = self._db)
        return user

forms.py

# from django.contrib.auth import get_user_model
from accounts.models import CustomUser
from django.contrib.auth.forms import UserCreationForm

class SignUpForm(UserCreationForm):

    class Meta:
        fields = ('user_type','account_type','email', 'password1', 'password2')
        model = CustomUser()

    # def __init__(self, *args, **kwargs) -> None:
    #     super().__init__(*args, **kwargs)

admin.py

from django.contrib import admin
from accounts.models import CustomUser

# Register your models here.
admin.site.register(CustomUser)

我是django开发的新手,请帮帮我。
我希望知道如何解决这种错误

c7rzv4ha

c7rzv4ha1#

文件中的错误:

class SignUpForm(UserCreationForm):

    class Meta:
        fields = ('user_type','account_type','email', 'password1', 'password2')
        model = CustomUser()

固定到

class SignUpForm(forms.ModelForm):

    class Meta:
        fields = ('user_type','account_type','email', 'password1', 'password2')
        model = CustomUser

因为model需要一个MODEL,你写CustomUser()意味着可调用对象,模型CustomUser不能这样做并引发异常

相关问题