属性错误:在Django中,'Manager'对象没有属性'get_by_natural_key'错误,

z0qdvdin  于 2023-01-27  发布在  Go
关注(0)|答案(4)|浏览(215)

我正在使用Django '1.5c1'。我的www.example.com中有这行代码settings.py:

AUTH_USER_MODEL = 'fileupload.galaxyuser'

下面是我的Galaxyuser模型:

class GalaxyUser(models.Model):
    id = models.IntegerField(primary_key=True)
    create_time = models.DateTimeField(null=True, blank=True)
    update_time = models.DateTimeField(null=True, blank=True)
    email = models.CharField(max_length=765)
    password = models.CharField(max_length=120)
    external = models.IntegerField(null=True, blank=True)
    deleted = models.IntegerField(null=True, blank=True)
    purged = models.IntegerField(null=True, blank=True)
    username = models.CharField(max_length=765, blank=True)
    form_values_id = models.IntegerField(null=True, blank=True)
    disk_usage = models.DecimalField(null=True, max_digits=16, decimal_places=0, blank=True)
    class Meta:
        db_table = u'galaxy_user'

我想从Galaxyuser模型进行身份验证。但是当我登录时,我收到这个错误:

AttributeError: 'Manager' object has no attribute 'get_by_natural_key'

我哪里做错了?

    • 编辑:**Traceback:
Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/zurelsoft/workspace/genalytics/fileupload/backend.py" in login_backend
  26.         user = authenticate(username=username, password=password)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/__init__.py" in authenticate
  59.             user = backend.authenticate(**credentials)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/backends.py" in authenticate
  16.             user = UserModel.objects.get_by_natural_key(username)

Exception Type: AttributeError at /login_backend/
Exception Value: 'Manager' object has no attribute 'get_by_natural_key'
p1iqtdky

p1iqtdky1#

您已经创建了一个新的用户模型,但是还没有为该模型指定管理者。如果您还不熟悉Django中的管理者,我建议您先阅读documentation on that。正如Django 1.5所说(source):
你也应该为你的用户模型定义一个自定义管理器,如果你的用户模型定义的用户名和email字段和Django的默认用户一样,你可以直接安装Django的UserManager;但是,如果用户模型定义了不同的字段,则需要定义一个自定义管理器来扩展BaseUserManager,提供两个附加方法:create_user()create_superuser()
简而言之,如果你的模型和Django的User模型使用相同的用户名和email字段,那么你可以写:

from django.contrib.auth.models import UserManager

class GalaxyUser(models.Model):
    id = models.IntegerField(primary_key=True)
    create_time = models.DateTimeField(null=True, blank=True)
    update_time = models.DateTimeField(null=True, blank=True)
    email = models.CharField(max_length=765)
    password = models.CharField(max_length=120)
    external = models.IntegerField(null=True, blank=True)
    deleted = models.IntegerField(null=True, blank=True)
    purged = models.IntegerField(null=True, blank=True)
    username = models.CharField(max_length=765, blank=True)
    form_values_id = models.IntegerField(null=True, blank=True)
    disk_usage = models.DecimalField(null=True, max_digits=16, decimal_places=0, blank=True)

    objects = UserManager()

    class Meta:
        db_table = u'galaxy_user'

或者,你需要子类化BaseUserManager(也在django.contrib.auth.models中)并实现所需的方法,然后,你需要将它赋给模型的objects变量。

iaqfqrcu

iaqfqrcu2#

如果GalaxyUser类有一个UserManger类,请确保将objects = UserManager()添加到该类中

o2gm4chl

o2gm4chl3#

您必须为自定义模型创建一个用户管理器,可能是GalaxyUserManager,然后将objects = GalaxyUserManager()添加到您的用户模型中

jyztefdp

jyztefdp4#

对我来说..我忘了在经理的末尾添加Parentheses

objects = UserManager

objects = UserManager()

相关问题