Zamphatta
Zamphatta

Reputation: 4724

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

I'm using Django 1.5 on Python 3.2.3.

When I run python3 manage.py syncdb, it builds the DB tables, & asks for my email (defined as the unique instead of a username), and then when I enter that, I get this error --

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

Oddly, it creates the tables anyway, but now, I'm confused 'cause I really don't get what I'm supposed to do. The documentation says I should create a Custom User Manager, but that's all it really says. It doesn't give me a clue where to create it or how. I looked through the docs on Managers, but that really didn't help me figure anything out. It's all too vague. I keep Googling trying to find some clue as to what I need to do, but I'm just going crazy with more and more questions instead of any answer. Here's my models.py file:

from django.db import models
from django.contrib.auth.models import AbstractBaseUser

class MyUsr(AbstractBaseUser):
    email = models.EmailField(unique=True,db_index=True)
    fname = models.CharField(max_length=255,blank=True, null=True)
    surname = models.CharField(max_length=255,blank=True, null=True)
    pwd_try_count = models.PositiveSmallIntegerField(blank=True, null=True)
    pwd_last_try = models.DateTimeField(blank=True, null=True)
    resetid = models.CharField(max_length=100,blank=True, null=True)
    last_reset = models.DateTimeField(blank=True, null=True)
    activation_code = models.CharField(max_length=100,blank=True, null=True)
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['fname','activation_code']

How do I write a Custom User Manager? Do I put it in the MyUsr model as a method? Or is that even what I'm supposed to do? Should I be doing something totally different? I'm not sure of anything at this point. I just don't understand. The documentation on this doesn't seem clear to me, leaving a lot open for interpretation. I'm pretty new to Django, but I've been into Python for several months.

Upvotes: 4

Views: 15302

Answers (2)

benAv
benAv

Reputation: 55

Depending on your case all you might need to fix this error will be to assign the objects variable in the CustomUser class, to inherit form BaseUserManager() instead of models.Manager() which some would have initially set

from django.db import models
from django.contrib.auth.models import AbstractUser, BaseUserManager

class CustomUser(AbstractUser):
    name = models.CharField(null=True, blank=True, max_length=100)
    ......
    # model managers
    # objects = models.Manager()
    objects = BaseUserManager()

Upvotes: 0

pztrick
pztrick

Reputation: 3831

You define a custom manager by sub-classing BaseUserManager and assigning it in your Model to the objects attribute.

from django.contrib.auth.models import AbstractUser, BaseUserManager

class MyMgr(BaseUserManager):
    def create_user(...):
        ...

    def create_superuser(...):
        ...


class MyUsr(AbstractBaseUser):
    objects = MyMgr()

    email = models.EmailField(unique=True, db_index=True)
    fname = models.CharField(max_length=255, blank=True, null=True)
    ...

You must define the create_user and create_superuser methods for your BaseUserManager. See the docs.

Upvotes: 14

Related Questions