django-registration create_user() 意外关键字

标签 django django-registration django-profiles

我正在尝试创建自定义配置文件以将其他字段添加到 django-registration。这是我到目前为止的代码 --

models.py

from django.db import models
from django.contrib.auth.models import User

class Profile(models.Model):
    user = models.ForeignKey(User, unique=True)
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    email = models.CharField(max_length=50)
    password = models.CharField(max_length=50) 

并在 settings.py

AUTH_PROFILE_MODULE = 'myproject.Profile'

但是,当我尝试使用 create_user() 时出现以下错误。这是我将其输入解释器时发生的情况 --

>>> from django.contrib.auth.models import User
>>> User.objects.create_user(first_name='tom',last_name='smith',email='ts@gmail.com',password='hello')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: create_user() got an unexpected keyword argument 'first_name'

为了让 create_user() 识别这些新列,我缺少什么?谢谢。

最佳答案

这里有很多地方你做错了

首先,这些不是 User 模型中的字段,它们是 Profile 模型中的字段。因此,用户管理器方法无法识别它们也就不足为奇了。您需要使用 create_user 以正常方式定义一个 User 对象,然后使用该用户定义 Profile 实例。

其次,create_user 不会将所有字段名称作为关键字参数 - 它只需要 username, email and password .同样,之后您需要自己设置其他字段。

但是,我完全不明白您为什么要定义这些特定字段。它们都可以在 built-in User model 中找到.特别是,您绝对不应该定义自己的密码字段,除非您还编写一些代码将密码存储为哈希而不是纯文本。

关于django-registration create_user() 意外关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6007556/

相关文章:

Django rest-auth注册。使用 token 认证时如何通过 key 返回user_id

python - Django 1.5 的多种用户类型、自定义字段和共享身份验证

python - 数据如何连接到特定的用户 ID?

geodjango - Django-profiles,带有地理字段的用户配置文件?

django - 部署 Django 项目的不同方法及其优缺点?

python - 南值错误: Cannot successfully create field for model: 'module' object has no attribute <CustomClassAttributeHere>

python - Django CMS - 无法通过 cmsplugin_filer_image 上传图像

python - 在 CreateView 上制作表单字段的正确 Django 方法,但在 UpdateView 上是可选的?