【问题标题】:something about the django.contrib.auth.models source code关于 django.contrib.auth.models 源代码的一些东西
【发布时间】:2015-12-04 03:43:01
【问题描述】:

这是我提出的最后一个问题,但它仍然是错误的方式。 it is here 。所以

回溯在这里:

>>>from app1.models import UserReg

>>> UserReg.objects.create_user(username=u'hello world')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python27\lib\site-packages\django\contrib\auth\models.py", line 187,
in create_user
    **extra_fields)
  File "C:\Python27\lib\site-packages\django\contrib\auth\models.py", line 180,
in _create_user
    date_joined=now, **extra_fields)
TypeError: __init__() got an unexpected keyword argument 'username'

我在 django 包中看到了 models.py,但似乎我没有错。所以如果你能指出我在 django 的 AbstracteUser 模型中的错误之处,我会很高兴。

如果你有什么需要我补充的,我会的。请 :((.

【问题讨论】:

    标签: python django django-models


    【解决方案1】:

    您的模型中没有名为 username 的字段,因此您无法将其传递给 __init__ 方法。

    【讨论】:

      【解决方案2】:

      我认为,UserReg 是您的 UserProfile 模型,它与来自 auth 应用程序的 django 用户模型具有 OneToOne 关系。

      那么你需要先使用django的用户模型创建一个新用户

      from django.contrib.auth.models import User
      
      user = User.objects.create_user(username='hello world')
      
      userreg = UserReg(user=user) 
      # we create UserReg instance for 'user'
      # if UserReg has a 'user' OneToOne Field 
      
      userreg.save()
      

      【讨论】:

      • 为什么我不能使用 AbstracteUser ,你有一些关于 AbstracteUser 用法的文章吗?我会试试这个
      • @Bhhaha 不需要 abstractuser。只需使用用户模型
      • 但是我使用abstracteuser扩展了默认的User模型,似乎还有另一个属性错误。你可以看到我给的链接。
      • @Bhhaha 不使用 abstractuser 扩展用户模型。这是不正确的。使用普通用户模型扩展它
      • 你的意思是我应该使用 AdstracteBaseUser 而不是 AbstracteUser?如果是的话,我会试试的。
      【解决方案3】:

      对于这个愚蠢的问题,我感到非常抱歉,但与此同时,我很感激所有答案。在过去的两天里,我看起来差不多一百 现实问题。谢谢。

      ==========================================

      忘记了__init__()的用途,我在__init__()中插入了一个名为other name但没有命名为username的参数,所以在链接中可以看到回溯。

      最后,谢谢,我很喜欢 django。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-26
        • 1970-01-01
        • 2014-11-06
        相关资源
        最近更新 更多