无法创建超级用户:错误,没有这样的列。

huangapple go评论125阅读模式
英文:

Can't make superuser: error no such column

问题

在尝试登录我的管理员面板时,我尝试创建超级用户,但在创建超级用户时遇到了以下错误:

django.db.utils.OperationalError: no such column: accounts_user.password1

这是我的模型:

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

class User(AbstractUser):
    username = models.CharField(unique=True, max_length=10)
    email = models.EmailField()
    password1 = models.CharField(max_length=20)
    password2 = models.CharField(max_length=20)
    is_publisher = models.BooleanField(default=False)

    def change_user_type(self):
        if self.is_publisher:
            self.is_superuser = True
        self.save()
英文:

Hi I try make superuser to login my admin panel but I recieved this error while making superuser:

django.db.utils.OperationalError: no such column: accounts_user.password1

and this is my model:

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


class User(AbstractUser):
    username = models.CharField(unique=True, max_length=10)
    email = models.EmailField()
    password1 = models.CharField(max_length=20)
    password2 = models.CharField(max_length=20)
    is_publisher = models.BooleanField(default=False)

    def change_user_type(self):
        if self.is_publisher:
           self.is_superuser = True
        self.save()

答案1

得分: 1

你不需要在数据库中存储两个密码。只需在前端验证中检查它们。由于您自动继承了AbstractUser,因此默认情况下会获得密码字段。

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

class User(AbstractUser):
    email = models.EmailField(unique=True)
    is_publisher = models.BooleanField(default=False)

    def change_user_type(self):
        if self.is_publisher:
            self.is_superuser = True
        self.save()
英文:

You do not need to store two password in db. Just check those in FE validation.Since you inherit the AbstractUser automatically you are getting password field by default.

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


class User(AbstractUser):
      email = models.EmailField(unique=True)
      is_publisher = models.BooleanField(default=False)
  
      def change_user_type(self):
          if self.is_publisher:
             self.is_superuser = True
          self.save()

huangapple
  • 本文由 发表于 2023年3月8日 17:10:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671169.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定