Django自定义用户模型和usermanager [英] Django custom user model and usermanager
问题描述
我正在使用Django 1.5构建一个Web应用程序。我正在使用带有自定义UserManager的自定义用户模型。
我遵循了Django官方文档的说明和示例。
i'm building a web application with Django 1.5. I'm using a custom User model with a custom UserManager. I followed the instructions and examples of the official Django documentation.
现在,当我尝试通过 UserManager.create_user(...)
创建新用户时正在收到NoneType错误:似乎UserManager的属性模型的类型为None。
我想我在用户模型中正确设置了UserManager( objects = UserManager()
)
Now, when i'm trying to create a new user via UserManager.create_user(...)
i'm getting a NoneType error: It seems the UserManager's attribute models is of type None.
I think i'm setting up the UserManager correctly in the User model ( objects = UserManager()
)
我真的不知道我在哪里犯错。展位我的编码合作伙伴和我是Django的新手。
I really don't know where i'm making a mistake. Booth my coding partners and i are new to Django. Maybe you can help us out.
这是代码:
class UserManager(BaseUserManager):
"""
create a new user
@param username: the name for the new user
@param password: the password for the new user. if none is provided a random password is generated
@param person: the corresponding person object for this user
"""
def create_user(self, username, person, password=None):
if not username:
raise ValueError('User must have a valid username')
user = self.model(username=username, created=datetime.now(), must_change_password=True, deleted=False, person=person)
user.set_password(password)
user.save(using=self._db)
return user
class User(AbstractBaseUser):
## the id of the user. unique through the application
user_id = models.AutoField(primary_key=True)
## the name of the user. unique through the application
username = models.CharField(max_length=32, unique=True)
## the date when the user was created
created = models.DateTimeField()
## iff this is true the user must set a new password at next login
must_change_password = models.BooleanField(default=True)
## iff true the user is marked as deleted and can not login
deleted = models.BooleanField(default=False)
## iff true the user is admin and has all permissions. use with care!
is_admin = models.BooleanField(default=False)
## reference to the person entity that is linked to this specific user
person = models.ForeignKey(Person)
## indicates if the user is active or not
active = models.BooleanField(default=True)
## define the user manager class for User
objects = UserManager()
# necessary to use the django authentication framework: this field is used as username
USERNAME_FIELD = 'username'
我在UserManager的create_user()方法中的 user = self.model(..)
行出现了NoneType错误
I'm getting the NoneType Error at line user = self.model(..)
in the create_user() method in the UserManager
推荐答案
我在保存自定义用户模型时也遇到了问题,花了我一些时间才能弄清楚我们的
I also had problems saving the custom user model and it took me while to figure it our
我认为您代码中的重要行是:
I think the important line in your code is:
objects = UserManager()
在User类中,所以我n为了保存新用户,您需要致电
within the User class, so in order to save the new user you need to call
new_user=User.objects.create_user(args, args, args, etc)
对象
是项目调用 UserManager
类,并在 django
the "objects
" is the item that calls the UserManager
class and is called a manager in django
这篇关于Django自定义用户模型和usermanager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!