为什么Django给我:'first_name'是这个函数的无效关键字参数? [英] Why is Django giving me: 'first_name' is an invalid keyword argument for this function?

查看:342
本文介绍了为什么Django给我:'first_name'是这个函数的无效关键字参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个自定义用户配置文件,并且仅仅从django.conf导入设置稍微修改了一个例子

I'm trying to create a custom user profile and have modified the example only slightly

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时,会出现以下错误:

When I try to run syncdb I get the following error:

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

由于错误信息的本质有限,我一直在努力调试。我觉得我犯了一个简单的错误,我做错了什么?

I have struggled to debug this because of the limited nature of the error message. I feel I'm making a simple mistake what am I doing wrong?

推荐答案

你正在尝试设置None(Null)进入first_name,似乎该属性不允许。

You are trying to set None (Null) value into first_name and it seems that this property don't allow it.

尝试此更改:

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

在型号中:

first_name=models.CharField(max_length = 30, blank = True)

这篇关于为什么Django给我:'first_name'是这个函数的无效关键字参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆