无法使用自定义userprofile创建超级用户

时间:2013-06-27 12:05:16

标签: django

我尝试“./manage.py syncdb”,我被提示创建一个超级用户,但它失败了:

  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/<USER>/.virtualenvs/<DOMAIN>/src/django-trunk/django/core/management/__init__.py", line 397, in execute_from_command_line
    utility.execute()
  File "/home/<USER>/.virtualenvs/<DOMAIN>/src/django-trunk/django/core/management/__init__.py", line 390, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/<USER>/.virtualenvs/<DOMAIN>/src/django-trunk/django/core/management/base.py", line 240, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/<USER>/.virtualenvs/<DOMAIN>/src/django-trunk/django/core/management/base.py", line 283, in execute
    output = self.handle(*args, **options)
  File "/home/<USER>/.virtualenvs/<DOMAIN>/src/django-trunk/django/contrib/auth/management/commands/createsuperuser.py", line 141, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
TypeError: create_superuser() takes exactly 4 arguments (3 given)

这是我的userprofile:

from django.db import models
from django.conf import settings
from django.contrib.auth.models import UserManager, AbstractUser



class UserProfile(AbstractUser):
    membernumber = models.CharField(max_length=20, blank=True, unique=True)
    mobile = models.CharField(max_length=20, blank=True)
    updated = models.DateTimeField(blank=True, null=True)    

    USERNAME_FIELD = 'membernumber'

    objects = UserManager()

也许问题是我在独特字段上运行“syncdb”并且“blank = True”?

2 个答案:

答案 0 :(得分:1)

您应该定义自己的经理而不是默认的UserManager;

作为你的例子:

class MyManager(UserManager):
    def create_superuser(self, membernumber,email,password,**extra_fields):
        super().create_superuser(membernumber,email,password,**extra_fileds)
        #or do something to save your model by yourself;

为什么我们需要这样做?

只看你的错误转储

self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)

因此,当您在shell上运行python manage.py createsuperuser时,在输入membernumberpasswordemail值后,您的user_data将变为:< / p>

{'membernumber':'Garreth','password':'123456','email':'like@gamil.com'}

但是UserManager.create_superuser需要username param,你的想念!所以你得到了那个错误。

以下是UserManager.create_superuser(参见1)

的来源
def create_superuser(self, username, email, password, **extra_fields):
    extra_fields.setdefault('is_staff', True)
    extra_fields.setdefault('is_superuser', True)

    if extra_fields.get('is_staff') is not True:
        raise ValueError('Superuser must have is_staff=True.')
    if extra_fields.get('is_superuser') is not True:
        raise ValueError('Superuser must have is_superuser=True.')

    return self._create_user(username, email, password, **extra_fields)

关于你python manage.py createsuperuser(参见2)

时运行的一些消息来源
if username:
    user_data[self.UserModel.USERNAME_FIELD] = username
    user_data['password'] = password

    self.UserModel._default_manager.db_manager(database)
        \.create_superuser(**user_data)
    if options['verbosity'] >= 1:
        self.stdout.write("Superuser created successfully.")

建议:

如果您仍想执行python manager.py createsuperuser,则应从不定义继承AbstractBaseUser的自定义UserModel,因为django.contrib.admindjango.contrib.auth使用AbstractUseris_activeis_superuser模型太多了,所以当您在浏览器上访问yousite.com/admin时会出现很多错误。

指:

1。https://github.com/django/django/blob/master/django/contrib/auth/models.py#L156

2。https://github.com/django/django/blob/master/django/contrib/auth/management/commands/createsuperuser.py#L170

答案 1 :(得分:0)

在此book中说:

  

选项2:Subclass AbstractUser:

     
    

如果您喜欢Django的用户模型字段,请选择此选项,但需要额外的字段。

  

也许你必须继承AbstractBaseUser。请参阅here或完整示例。

<强> 修改

如果您想使用其他用户名字段,则应使用

USERNAME_FIELD。如果您需要,则必须继承AbstractBaseUser而不是AbstractUser,因为最后一个已经定义了USERNAME_FIELD

希望有所帮助!