2

カスタム ユーザー プロファイルを作成しようとしていて、例を少しだけ変更しました

from django.conf import settings
from django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser

class MyUserManager(BaseUserManager):
    def create_user(self, email, password=None, first_name=None, last_name=None):

        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            email=MyUserManager.normalize_email(email),
            first_name=first_name,
            last_name=last_name,
            #date_of_birth=date_of_birth,
        )

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

    def create_superuser(self, email, password, first_name=None, last_name=None):

        user = self.create_user(email,
            password=password,
            first_name=first_name,
            last_name=last_name,            
            #date_of_birth=date_of_birth
        )
        user.is_admin = True
        user.save(using=self._db)
        return user

class MyUser(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
        db_index=True,
    )
    first_name=models.CharField(max_length = 30),
    last_name=models.CharField(max_length = 30),    
    #date_of_birth = models.DateField()
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = MyUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    def get_full_name(self):
        # The user is identified by their email address
        return self.email

    def get_short_name(self):
        # The user is identified by their email address
        return self.email

    def __unicode__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        "Does the user have a specific permission?"
        # Simplest possible answer: Yes, always
        return True

    def has_module_perms(self, app_label):
        "Does the user have permissions to view the app `app_label`?"
        # Simplest possible answer: Yes, always
        return True

    @property
    def is_staff(self):
        "Is the user a member of staff?"
        # Simplest possible answer: All admins are staff
        return self.is_admin

syncdb を実行しようとすると、次のエラーが表示されます。

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Email address: uou@pce.com
Password:
Password (again):
TypeError: 'first_name' is an invalid keyword argument for this function

エラー メッセージの性質が限られているため、これをデバッグするのに苦労しました。私は単純な間違いを犯しているように感じます。何が間違っているのでしょうか?

4

1 に答える 1

3

なし (Null) 値を first_name に設定しようとしていますが、このプロパティでは許可されていないようです。

この変更を試してください:

class MyUserManager(BaseUserManager):
    def create_user(self, email, password=None, first_name='', last_name=''):

モデル内:

first_name=models.CharField(max_length = 30, blank = True)
于 2013-05-29T07:38:24.817 に答える