简体   繁体   English

我正在尝试在 django 中制作 model。 但错误。 为什么?

[英]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参数等。您将这些作为usernameuserid等传递:

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 user_id .注意:通常 Django model 中的字段名称是用snake_case 写的,而不是 PerlCase 所以它应该是: UserId而不是user_id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试使用 django 登录。 但总是失败。 为什么? - I'm trying to login with django. but always fail. why? 请我是 python 的新手,我正在尝试使用 Django 做一个食谱应用程序。 我遇到了一个错误,即“UnboundLocalError:局部变量形式 - Please I'm new to python and I'm trying to do a recipe app using Django. I encountered an error which is "UnboundLocalError: local variable form 我正在尝试将 celery 应用于 django 中基于 Class 的视图(apis)。 我怎样才能做到这一点? - I'm trying to apply celery to Class based view(apis) in django. How can I do this? Django的。 为什么我不能将参数从抽象模型类传递到子模型字段? - Django. Why I cant pass parametr from abstract model class to child model field? Django的。 如何从其他模型中进行模型选择 - Django. How to make model choices from another model 我正在尝试为我正在开发的应用程序制作 GUI,但是当我尝试打印全局变量时出现错误。 为什么? - I am trying to make a GUI for a app I am working on but when I try print a global variable I get an error. Why? Django的。 远程数据库上auth模型上的Unicode BUG /错误 - Django. A Unicode BUG/Error on auth model on a remote DB 为什么我在Django中收到此错误(我正在尝试做未修改的304) - Why am I getting this error in Django (I'm trying to do a 304 not modified) django。 我该如何在模型中做一些数学运算 - django. how can i do some math in model 姜戈。 覆盖模型的保存 - Django. Override save for model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM