2017-11-18 8 views
0

私のdjangoアプリケーション用のカスタムユーザーモデルを作成しようとしています。 CustomUserとCustomUserManagerのコードは次のとおりです。誰も助けることができる場合Django - create_superuser()は予期しないキーワード引数 'user_type'を持っています

from django.utils.translation import ugettext_lazy as _ 

class CustomUserManager(BaseUserManager): 
def _create_user(self, anonymous, first_name, last_name, email, username, password, home_address, user_type, image_path): 
    now = timezone.now() 

    if not email: 
     raise ValueError('The gives email must be set') 

    email = self.normalize_email(email) 
    user = self.model(
     anonymous=anonymous, 
     first_name=first_name, 
     last_name=last_name, 
     email=email, 
     username=username, 
     home_address=home_address, 
     user_type=user_type, 
     image_path=image_path, 
     created_time=now, 
     last_login=now 
    ) 

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


    def create_superuser(self, first_name, last_name, email, username, password, home_address, image_path): 
    return self._create_user(False, first_name, last_name, email, username, password, home_address, 0, image_path) 

class CustomUser(AbstractBaseUser): 
    anonymous = models.BooleanField() 
    username = models.CharField(max_length=255, unique=True) 
    first_name = models.CharField(max_length=255, blank=True) 
    last_name = models.CharField(max_length=255, blank=True) 
    email = models.EmailField(blank=True, unique=True) 
    home_address = models.CharField(max_length=255, blank=True) 
    user_type = models.IntegerField(1) 
    image_path = models.CharField(max_length=500, blank=True) 
    created_time = models.TimeField() 

    USERNAME_FIELD = 'email' 
    REQUIRED_FIELDS = ['username', 'home_address', 'first_name', 'last_name', 'user_type'] 

    objects = CustomUserManager() 
    class Meta: 
     verbose_name = _('user') 
     verbose_name_plural = _('users') 

それから私は予期しない引数USER_TYPE

Traceback (most recent call last):

File "manage.py", line 22, in execute_from_command_line(sys.argv)

File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management__init__.py", line 363, in execute_ from_command_line utility.execute()

File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management__init__.py", line 355, in execute self.fetch_command(subcommand).run_from_argv(self.argv)

File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\base.py", line 283, in run_from_arg v self.execute(*args, **cmd_options)

File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py ", line 63, in execute return super(Command, self).execute(*args, **options)

File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\base.py", line 330, in execute output = self.handle(*args, **options)

File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\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() got an unexpected keyword argument 'user_type'

に関するエラーが出ますが、事前にありがとうございました!

+0

設定でこのカスタムモデルを 'AUTH_USER_MODEL'として設定しましたか? – schwobaseggl

+0

@schwobasegglはい、私はAUTH_USER_MODEL = 'CustomUser.CustomUser'でした。最初のCustomUserはアプリケーションです –

答えて

1

カスタムマネージャにカスタムマネージャを割り当てた場所がわかりません。これは、スタックトレースのデフォルトのマネージャーを見ているように見えるという事実によって示されます。カスタムマネージャをモデルas described in the docsに割り当ててみてください。

+0

こんにちは@MrName、私は混乱しています、プログラムがデフォルトのものを使用しようとしている場合、どのように入力user_typeを取得しますか?そしてCustomUserクラスにCustomUserクラスのCustomUserManager()があるので、CustomOneを使用する必要があると思います。 –

+0

元の投稿のコードを更新して現在の状態を反映できますか?貼り付けられたものは 'objects = CustomUserManager()'が表示されません – MrName

+0

が追加されました。不要なメソッドを隠すだけでなく、その行についても忘れてしまいたいです。 –

関連する問題