英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论