AttributeError: the 'Manager' object does not have the 'get_by_natural_key' attribute

I am using Django 1.5 on Python 3.2.3.

When I run python3 manage.py syncdb , it creates DB tables and requests my email address (defined as unique instead of username), and then when I enter it, I get this error -

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

Oddly enough, it creates tables anyway, but now I'm confused because I really don't understand what I should do. The documentation says that I have to create a Custom User Manager , but all this really says. It does not give me clues where to create it or how. I looked through the docs in Managers , but it really didn't help me understand anything. All of this is too vague. I continue to search Google, trying to find the key to what I need to do, but I'm just losing my mind because of more questions, not the answer. Here is 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 to write a user user manager? I put it in MyUsr model as a method? Or is this what I should do? Should I do something completely different? I don’t know anything at the moment. I just do not understand. The documentation on this subject does not seem clear to me, leaving a lot open for interpretation. I am new to Django, but I have been in Python for several months.

+4
source share
1 answer

You define a custom manager by subclassing BaseUserManager and assigning it to the objects attribute in your model.

 from django.contrib.auth.models import AbstractUser, UserManager 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 docs .

+13
source

Source: https://habr.com/ru/post/1480577/


All Articles