Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

python - AttributeError: 'Manager' object has no attribute 'get_by_natural_key' error in Django?

I am using Django '1.5c1'. I have this line in my settings.py:

AUTH_USER_MODEL = 'fileupload.galaxyuser'

Here's my 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:

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

What am I doing wrong?

Edit: 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'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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. As the Django 1.5 say (source):

You should also define a custom manager for your User model. If your User model defines username and email fields the same as Django's default User, you can just install Django's 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() and create_superuser().

In short, if your model uses the same username and email fields as Django's User model then you can write:

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. Then, you'll need to assign it to the objects variable for your model.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...