[英]I'm trying to make model in django. but error. why?
I'm newbie coder.我是新手编码员。 I'm trying to make model.我正在尝试制作 model。 but when i make superuser, always error appear this is my code.但是当我成为超级用户时,总是出现错误这是我的代码。 i use abstractbaseuser and create account.我使用 abstractbaseuser 并创建帐户。 my account contain username, userid, company, company_code.我的帐户包含用户名、用户 ID、公司、公司代码。 not use password.不使用密码。 but i don't know how to fix this error bold但我不知道如何解决这个错误粗体
from django.contrib.auth.models import(BaseUserManager, AbstractBaseUser, PermissionsMixin)
from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
class UserManager(BaseUserManager):
def create_account(self, username, userid, company, company_code, password=None):
if not username:
raise ValueError(_('Users must have an name!'))
user = self.model(
Name=username,
UserId = userid,
Company= company,
Code = company_code,
)
user.set_password(password)
user.save(self._db)
return user
def create_superuser(self, username, userid, company, company_code, password):
user = self.create_user(
Name=username,
UserId = userid,
Company = company,
Code = company_code,
password=password,
)
user.Name = True
user.is_superuser = True
user.is_admin = True
user.is_staff = True
user.save(self._db)
return user
class User(AbstractBaseUser):
Name = models.CharField(max_length=10, unique=True)
UserId = models.CharField(max_length=100, unique=True)
Company = models.CharField(max_length=10)
Code = models.CharField(max_length=100)
Created_date = models.DateTimeField(auto_now_add=True)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
class Meta:
db_table = 'user'
objects = UserManager()
USERNAME_FIELD = 'Name'
REQUIRED_FIELDS = ['UserId', 'Company', 'Code']
def __str__(self):
return self.Name
def get_full_name(self):
return self.Name
def get_short_name(self):
return self.Name
@property
def is_staff(self):
"Is the user a member of staff?"
return self.is_superuser
this is error when i commmand createsuperuser这是我命令 createsuperuser 时的错误
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 79, in execute
return super().execute(*args, **options)
File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 189, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
TypeError: create_superuser() got an unexpected keyword argument 'Name'
You call it with the wrong parameters.你用错误的参数调用它。 In you create_superuser
, you write:在你create_superuser
中,你写:
def create_superuser(self, username, userid, company, company_code, password):
user = self.create_user(
Name=username,
UserId=userid,
Company=company,
Code=company_code,
password=password,
)
# …
But your create_user
method has no Name
parameter, etc. You pass these as username
, userid
, etc.:但是您的create_user
方法没有Name
参数等。您将这些作为username
、 userid
等传递:
def create_superuser(self, username, userid, company, company_code, password):
user = self.create_user(
username=username,
userid=userid,
bompany=company,
company_code=company_code,
password=password,
)
# …
Note : normally the name of the fields in a Django model are written in snake_case , not PerlCase , so it should be:
UserId
instead of.注意:通常 Django model 中的字段名称是用snake_case 写的,而不是 PerlCase ,所以它应该是:user_id
UserId
而不是。user_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.