【问题标题】:Exception Value: 'CustomUser' object is not callable | CustomUserModel异常值:\'CustomUser\' 对象不可调用 |自定义用户模型
【发布时间】:2023-02-02 17:40:19
【问题描述】:

我正在尝试创建自定义用户模型。该模型在命令提示符下运行良好,我也能够登录到管理面板。我也可以访问登录页面。但是当我尝试访问 SignUp 页面时,我看到了以下错误。

Error image

模型.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})

视图.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'

管理者.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

表单.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)

管理员.py

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

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

我是 django 开发的新手。请帮帮我。

我期待知道如何解决这种错误

【问题讨论】:

    标签: django django-class-based-views django-custom-user callable-object


    【解决方案1】:

    你在文件中的错误:

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

    修复到

    model = CustomUser
    

    【讨论】:

      猜你喜欢
      • 2019-12-18
      • 2015-10-13
      • 2023-03-10
      • 1970-01-01
      • 2011-09-20
      • 1970-01-01
      • 2017-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多