AttributeError: 'Manager' object has no attribute 'get_by_natural_key' error in Django?
Asked Answered
S

4

38

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'
Selfabuse answered 6/2, 2013 at 6:41 Comment(4)
can you provide your settings.py filePravit
paste the track back alsoPravit
django will create id for you no need to add id in your model and if you are using the django user model then username must be OneToOne field for the User model check the docsPravit
The only thing I want to do is authenticate from the GalaxyUser model using the build in Django authentication function. I am getting error while doing do. ThanksSelfabuse
P
69

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.

Pibgorn answered 6/2, 2013 at 8:44 Comment(9)
I am getting this error after implementing this: type object 'User' has no attribute 'USERNAME_FIELD'Selfabuse
I have renamed my model to User from GalaxyUser.Selfabuse
I read the documentation you provided but I couldn't find the solution. Can you help me on this?Selfabuse
I have solved this using: USERNAME_FIELD = 'username' but I get another error: 'User' object has no attribute 'check_password'Selfabuse
I think you can solve that by extending your user model from AbstractBaseUser: docs.djangoproject.com/en/1.5/topics/auth/customizing/…Pibgorn
Did like this: class User(AbstractBaseUser) but got another error: FieldError: Local field 'password' in class 'User' clashes with field of similar name from base class 'AbstractBaseUser'Selfabuse
Since I want to authenticate from another table I don't want to subclass AbstractBaseUser.Selfabuse
some pretty unhelpful comments here - if we've landed here, then we're not understanding the docs.Persona
The link to the managers docs is outdated; here is the (2020) new version: https://docs.djangoproject.com/en/3.0/topics/db/managers/Flitting
C
5

for me.. I had forgotten to add Parentheses end of manager..

objects = UserManager

objects = UserManager()
Clarisclarisa answered 23/1, 2023 at 8:14 Comment(1)
My problem was I didn't even have "objects = Usermanager()" or any variation of it. Adding this close to the bottom of the class and making sure to use "from django.contrib.auth.models import UserManager" solved it for me. This answer helped me notice it.Whereupon
D
1

If you have a UserManger class for your GalaxyUser class, make sure you add objects = UserManager() to that

Disentomb answered 19/11, 2021 at 19:20 Comment(0)
P
1

You have to create a user manager for the custom model. Maybe GalaxyUserManager. Then add objects = GalaxyUserManager() to your user model

Portamento answered 1/4, 2022 at 14:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.