Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom user model in admin

Tags:

django

i try to create a custom user model for adding some custom fields to a user. i used the in django 1.5 introduced new method based on AbstractBaseUser. Everything (login) works, except for the admin-panel. When logging into the admin-interface, i get the following error:

AttributeError at /admin/
'ShopUser' object has no attribute 'is_superuser'

here's my model:

from django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
from django.contrib import auth

class ShopUserManager(BaseUserManager):

  def create_user(self, email, password=None):
    if not email:
      raise ValueError("We need an e-mail here...")

    user = self.model(
      email = ShopUserManager.normalize_email(email),
    )

    user.set_password(password)
    user.save(using=self._db)
    return user

  def create_superuser(self, email, password):
    user = self.create_user(
      email,
      password = password,
    )
    user.is_admin = True
    user.is_staff = True
    user.save(using=self._db)
    return user


class ShopUser(AbstractBaseUser):
  email = models.EmailField(
    verbose_name = 'e-mail address',
    max_length = 255,
    unique = True,
    db_index = True,
  )

  is_active = models.BooleanField(default=True)
  is_admin = models.BooleanField(default=False)
  is_staff = models.BooleanField(default=False)

  objects = ShopUserManager()

  USERNAME_FIELD = 'email'
  # REQUIRED_FIELDS = ['']

  def __unicode__(self):
    return self.email

  def has_perms(self, perm_list, obj=None):
    """
    Returns True if the user has each of the specified permissions. If
    object is passed, it checks if the user has all required perms for this
    object.
    """
    for perm in perm_list:
        if not self.has_perm(perm, obj):
            return False
    return True

  def has_module_perms(self, app_label):
    """
    Returns True if the user has any permissions in the given app label.
    Uses pretty much the same logic as has_perm, above.
    """
    # Active superusers have all permissions.
    if self.is_active and self.is_superuser:
        return True

    return _user_has_module_perms(self, app_label)

any advice on this? thanks!

like image 533
trnc Avatar asked Apr 11 '13 07:04

trnc


2 Answers

You don't have to let your class inherit from PermissionsMixin.

I had the same problem and I fixed it by adding a few required methods to my user class (in your case ShopUser).

class ShopUser(AbstractBaseUser):

  ...


  def get_full_name(self):
    return self.fullname

  def get_short_name(self):
    return self.shortname

  @property
  def is_superuser(self):
    return self.is_admin

  @property
  def is_staff(self):
    return self.is_admin

  def has_perm(self, perm, obj=None):
    return self.is_admin

  def has_module_perms(self, app_label):
    return self.is_admin
like image 89
Xing Gu Avatar answered Nov 12 '22 01:11

Xing Gu


Let the class ShopUser inherit from PermissionsMixin like so:

class ShopUser(AbstractBaseUser, PermissionsMixin):

This will add the is_superuser field and should play nicely with the admin UI.

Your create_superuser method should set user.is_superuser = True.

And finally, you should implement get_full_name and get_short_name for your ShopUser class.

More info here: Customizing authentication in Django

like image 35
qingu Avatar answered Nov 12 '22 02:11

qingu