【发布时间】:2016-11-03 07:35:32
【问题描述】:
我知道 SO 中有关于此问题的问题。但大多数问题都与AbstractBaseUser 有关。我没有找到AbstractUser 的任何问题。
问题:
我想为 django 项目实现身份验证。所以我想通过继承AbstractUser来实现自定义用户模型。
这是我的模型:
class User(AbstractUser):
phonenumber = models.CharField(max_length=25,unique=True)
username = models.CharField(max_length=25,default="")
profile_path = models.URLField(max_length=1500)
country = models.CharField(max_length=100,default="")
what_do_you_do = models.CharField(max_length=500,default="")
where_do_you_do = models.CharField(max_length=500,default="")
time_stamp = models.DateTimeField(auto_now_add=True,blank=True)
USERNAME_FIELD = 'phonenumber'
我在 settings.py 中添加了AUTH_USER_MODEL = 'XXX.User'。我想创建一个超级用户。
python manage.py createsuperuser
但它给了我以下错误:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/sandesh/server/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/home/sandesh/server/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/sandesh/server/local/lib/python2.7/site-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/sandesh/server/local/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 63, in execute
return super(Command, self).execute(*args, **options)
File "/home/sandesh/server/local/lib/python2.7/site-packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
File "/home/sandesh/server/local/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 183, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
TypeError: create_superuser() takes exactly 4 arguments (3 given)
【问题讨论】:
-
只是确保 - 您是否应用了迁移?
-
是的。我做到了。我没有实现 UserManager。这个错误与@ApoorvKansal 有什么关系
-
您使用的是哪个版本的 Djagno?如果 >=1.8,您需要将 PermissionsMixin 传递给您的 User 类
-
您应该创建自己的用户管理器。
标签: python django django-models django-rest-framework