Django 1.5: Understanding of AbstractBaseUser and permissions

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

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>.

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
    ...

Answers


Just add PermissionsMixin to your model declaration! :)

class MyUser(AbstractBaseUser, PermissionsMixin):
    ...

Here is the relevant part of Django docs.


Need Your Help

Inno Setup Acces violation increment array of strings

arrays string inno-setup pascal

I hava an array of strings. I increment this array when it is necessary with the function:

How to Remove and add a key which should be added in last location?

c# .net

When I remove Dictionary key add then add new key to dictionary new value is not added to last location. Instead of its adding in the key where it is removed.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.