[英]AttributeError: 'Manager' object has no attribute 'get_by_natural_key' error in Django?
I am using Django '1.5c1'
.我正在使用 Django
'1.5c1'
。 I have this line in my settings.py:我的 settings.py 中有这一行:
AUTH_USER_MODEL = 'fileupload.galaxyuser'
Here's my Galaxyuser
model:这是我的
Galaxyuser
model:
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'
I want to authenticate from Galaxyuser model. However when I login I am getting this error:我想从 Galaxyuser model 进行身份验证。但是,当我登录时出现此错误:
AttributeError: 'Manager' object has no attribute 'get_by_natural_key'
What am I doing wrong?我究竟做错了什么?
Edit: Traceback:
编辑:
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'
You have created a new user model but you have not yet specified a manager for that model.您已创建新的用户模型,但尚未为该模型指定管理员。 If you're not yet familiar with managers in Django I suggest reading the documentation on that first.
如果您还不熟悉 Django 中的管理器,我建议您先阅读相关文档。 As the Django 1.5 say ( source ):
正如 Django 1.5 所说( 来源):
You should also define a custom manager for your User model.
您还应该为您的 User 模型定义一个自定义管理器。 If your User model defines username and email fields the same as Django's default User, you can just install Django's UserManager;
如果你的 User 模型定义了与 Django 的默认 User 相同的 username 和 email 字段,你可以只安装 Django 的 UserManager; however, if your User model defines different fields, you will need to define a custom manager that extends BaseUserManager providing two additional methods:
create_user()
andcreate_superuser()
.但是,如果您的 User 模型定义了不同的字段,您将需要定义一个自定义管理器来扩展 BaseUserManager 提供两个额外的方法:
create_user()
和create_superuser()
。
In short, if your model uses the same username and email fields as Django's User
model then you can write:简而言之,如果您的模型使用与 Django 的
User
模型相同的用户名和电子邮件字段,那么您可以编写:
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'
Alternatively, you'll need to subclass BaseUserManager
(also in django.contrib.auth.models
) and implement the desired methods.或者,您需要
BaseUserManager
(也在django.contrib.auth.models
)并实现所需的方法。 Then, you'll need to assign it to the objects
variable for your model.然后,您需要将其分配给模型的
objects
变量。
如果您的 GalaxyUser 类有一个 UserManger 类,请确保将 objects = UserManager() 添加到该类
You have to create a user manager for the custom model. Maybe GalaxyUserManager
.您必须为自定义 model 创建一个用户管理器。也许
GalaxyUserManager
。 Then add objects = GalaxyUserManager()
to your user model然后将
objects = GalaxyUserManager()
添加到您的用户 model
for me.. I had forgotten to add Parentheses
end of manager..对我来说..我忘了在经理的结尾加上
Parentheses
..
objects = UserManager
objects = UserManager()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.