model - Django 1.5: Understanding of AbstractBaseUser and permissions -
i created user app myuser
extension of abstractbaseuser
. under impression model myuser
replace standard auth.user
, long mentioned in settings.py
auth_profile_module = 'profile.myuser'
my trouble can't set permissions users registered in myuser model. when try set group memberships , permissions, error user' instance expected, got <myuser: username>
.
how can add users of user model permissions , correct groups?
class myusermanager(baseusermanager): def create_user(self, username, email, phone, password=none, company=false): if not email: raise valueerror('users must have 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 user group , 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 ...
just add permissionsmixin
model declaration! :)
class myuser(abstractbaseuser, permissionsmixin): ...
here relevant part of django docs.
Comments
Post a Comment