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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With