I created an user app MyUser
which is an extension of AbstractBaseUser
. I was under the impression that the model MyUser
will replace the standard Auth.User
, as long as it is mentioned in settings.py
我创建了一个用户应用程序MyUser,它是AbstractBaseUser的扩展。我的印象是模型MyUser将取代标准的Auth.User,只要在settings.py中提到它。
AUTH_PROFILE_MODULE = 'profile.MyUser'
AUTH_PROFILE_MODULE ='profile.MyUser'
My trouble is now that I can't set the permissions for users registered in the MyUser model. When I try to set the group memberships and permissions, I get the error User' instance expected, got <MyUser: username>
.
我现在的麻烦是我无法为在MyUser模型中注册的用户设置权限。当我尝试设置组成员身份和权限时,我得到错误的用户'实例预期,得到
How can I add users of my user model to the permissions and correct groups?
如何将用户模型的用户添加到权限和正确的组?
class MyUserManager(BaseUserManager):
def create_user(self, username, email, phone, password=None, company=False):
if not email:
raise ValueError('Users must have an email address')
if not username: username = email.split('@')[0]
user = self.model(
email=MyUserManager.normalize_email(email),
username=username,phone=phone,)
user.set_password(password)
user.save(using=self._db)
# add to user group and set permissions
if company:
g = Group.objects.get(name='company')
p = Permission.objects.get(codename='add_company')
else:
g = Group.objects.get(name='user')
p = Permission.objects.get(codename='add_user')
g.user_set.add(user)
user.user_permissions.add(p)
return user
class MyUser(AbstractBaseUser):
username = models.CharField(max_length=254, unique=True, blank=True, null=True)
email = models.EmailField(max_length=254, unique=True, db_index=True)
phone = models.CharField(_('Phone Number'), max_length=25, blank=True, null=True,)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['phone']
objects = MyUserManager()
def get_full_name(self):
return self.email
...
2 个解决方案
#1
8
Just add PermissionsMixin
to your model declaration! :)
只需将PermissionsMixin添加到您的模型声明中即可! :)
class MyUser(AbstractBaseUser, PermissionsMixin):
...
Here is the relevant part of Django docs.
这是Django文档的相关部分。
#2
2
You've probably resolved your issue but for completeness did you mean to refer to setting AUTH_USER_MODEL vs AUTH_PROFILE_MODULE when creating a custom user model in Django 1.5.
你可能已经解决了你的问题但是为了完整性你是指在Django 1.5中创建自定义用户模型时参考设置AUTH_USER_MODEL和AUTH_PROFILE_MODULE。
ref: https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#substituting-a-custom-user-model
#1
8
Just add PermissionsMixin
to your model declaration! :)
只需将PermissionsMixin添加到您的模型声明中即可! :)
class MyUser(AbstractBaseUser, PermissionsMixin):
...
Here is the relevant part of Django docs.
这是Django文档的相关部分。
#2
2
You've probably resolved your issue but for completeness did you mean to refer to setting AUTH_USER_MODEL vs AUTH_PROFILE_MODULE when creating a custom user model in Django 1.5.
你可能已经解决了你的问题但是为了完整性你是指在Django 1.5中创建自定义用户模型时参考设置AUTH_USER_MODEL和AUTH_PROFILE_MODULE。
ref: https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#substituting-a-custom-user-model