Django中的自定义用户模型和表

时间:2018-12-03 05:25:08

标签: django

我知道用于存储用户名和密码的django默认表位于auth_user表中。我目前要使用自己的名为SysUser的表,所以我已经创建了用户模型和用户管理器。

这是我的代码:

model.py

class MyUserManager(BaseUserManager):
    def create_user(self, username, name,role,accesslevel, passwd=None):
        if not login:
            raise ValueError('Users must have an username')

        user = self.model(
            username=username,
            name=name,
            role=role,
            accesslevel=accesslevel,
        )

        user.set_password(passwd)
        user.save(using=self._db)
        return user

    def create_superuser(self, username, name,role,accesslevel, passwd):
        user = self.create_user(
            login,
            passwd=passwd,
            name=name,
            role=role,
            accesslevel=accesslevel,
        )
        user.is_admin = True
        user.save(using=self._db)
        return user

class SysUser(AbstractBaseUser):
    user_id = models.IntegerField(db_column='User_Id', primary_key=True)
    username = models.CharField(db_column='Username', max_length=50,unique=True) 
    name = models.CharField(db_column='Name', max_length=100)
    passwd = models.CharField(db_column='Passwd', max_length=30, )
    role = models.IntegerField(db_column='Role' ) 
    accesslevel = models.IntegerField(db_column='AccessLevel')

    objects = MyUserManager()

    USERNAME_FIELD = 'username'

    class Meta:
        db_table = 'SysUser'

我们可以看到,新表中的属性与默认表不同。但是创建用户并获得所有用户时出现错误。

这是我创建用户的代码:

>>> from test.models import SysUser
>>> user = SysUser.objects.create_user(username='simon',name='Simon',role='1',accesslevel='1',passwd='simon123')

这是我获取所有用户的代码:

user = SysUser.objects.all()
print(user)

这是错误返回:

Traceback:

File "C:\Python\Python36\lib\site-packages\django\db\backends\utils.py" in _execute
  85.                 return self.cursor.execute(sql, params)

The above exception (column SysUser.password does not exist
LINE 1: SELECT "SysUser"."password", "SysUser"."last_login", "Sys...
               ^
HINT:  Perhaps you meant to reference the column "SysUser.Passwd".
) was the direct cause of the following exception:

File "C:\Python\Python36\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)

File "C:\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\customers\views.py" in Supp
  974.     print(user)

File "C:\Python\Python36\lib\site-packages\django\db\models\query.py" in __repr__
  244.         data = list(self[:REPR_OUTPUT_SIZE + 1])

File "C:\Python\Python36\lib\site-packages\django\db\models\query.py" in __iter__
  268.         self._fetch_all()

File "C:\Python\Python36\lib\site-packages\django\db\models\query.py" in _fetch_all
  1186.             self._result_cache = list(self._iterable_class(self))

File "C:\Python\Python36\lib\site-packages\django\db\models\query.py" in __iter__
  54.         results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)

File "C:\Python\Python36\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  1065.             cursor.execute(sql, params)

File "C:\Python\Python36\lib\site-packages\django\db\backends\utils.py" in execute
  100.             return super().execute(sql, params)

File "C:\Python\Python36\lib\site-packages\django\db\backends\utils.py" in execute
  68.         return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)

File "C:\Python\Python36\lib\site-packages\django\db\backends\utils.py" in _execute_with_wrappers
  77.         return executor(sql, params, many, context)

File "C:\Python\Python36\lib\site-packages\django\db\backends\utils.py" in _execute
  85.                 return self.cursor.execute(sql, params)

File "C:\Python\Python36\lib\site-packages\django\db\utils.py" in __exit__
  89.                 raise dj_exc_value.with_traceback(traceback) from exc_value

File "C:\Python\Python36\lib\site-packages\django\db\backends\utils.py" in _execute
  85.                 return self.cursor.execute(sql, params)

Exception Type: ProgrammingError at /suppsummary/
Exception Value: column SysUser.password does not exist
LINE 1: SELECT "SysUser"."password", "SysUser"."last_login", "Sys...
               ^
HINT:  Perhaps you meant to reference the column "SysUser.Passwd".

如何引用“ SysUser.Passwd”列?

0 个答案:

没有答案