Custom User Model

161 views
Skip to first unread message

Manuel Buri

unread,
Apr 3, 2021, 12:54:31 PM4/3/21
to Django users
Hi, 

I have a custom user model (see below) with inheritance from AbstractBaseUser and PermissionMixin. Therefore I have not only the auth_group, auth_group_permissions and auth_permissions tables but also those for my custom user model called 'Account'.

Currently I have the following problem: I cannot add groups to the custom user model group but I can add to auth_group. Also in django admin looking at the custom user table I can see the auth_group... which is weird... I think I have done an error or missed something while setting it up... thank you very much for your help.

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.contrib.auth.models import User
from organization.models import Organization

class MyAccountManager(BaseUserManager):

def create_user(self, email, first_name, last_name, password=None):
if not email:
raise ValueError('Users must have an email address')
if not first_name:
raise ValueError('Users must have a first name')
if not last_name:
raise ValueError('Users must have a last name')

user = self.model(
email=self.normalize_email(email),
first_name=first_name,
last_name=last_name
)
user.set_password(password)
user.save(using=self._db)
return user

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


class Account(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(verbose_name="email", max_length=60, unique=True)
# username = models.CharField(max_length=30, unique=True)
# TODO: for production do not allow null field
first_name = models.CharField(verbose_name="first_name", max_length=40)
last_name = models.CharField(verbose_name="last_name", max_length=40)
full_name = models.CharField(verbose_name="full_name", max_length=80)
# 1 Employee has 1 Organization, but 1 Organization has many employees
organization = models.ForeignKey(Organization, on_delete=models.CASCADE, null=True)
date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
last_login = models.DateTimeField(verbose_name='last login', auto_now=True)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)


USERNAME_FIELD = "email"
REQUIRED_FIELDS = ["first_name", "last_name",]

objects = MyAccountManager()

def __str__(self):
return self.email

# For checking permissions. to keep it simple all admin have ALL permissions
def has_perm(self, perm, obj=None):
return self.is_admin

# Does this user have permission to view this app? (ALWAYS YES FOR SIMPLICITY)
def has_module_perms(self, app_label):
return True


Amitesh Sahay

unread,
Apr 4, 2021, 1:00:44 AM4/4/21
to Django users
Hello Manuel, 

Try the below. Recently I have worked on custom user model:

class AccountManager(BaseUserManager):
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', True)

if extra_fields.get('is_staff') is not True:
raise ValueError(_('Superuser must have is_staff=True.'))
if extra_fields.get('is_superuser') is not True:
raise ValueError(_('Superuser must have is_superuser=True.'))
return self.create_user(email, password, **extra_fields)

def create_user(self, email, password, **extra_fields):
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
if not email:
raise ValueError(_('Enter the email before proceeding'))

email = self.normalize_email(email)
user = self.model(email=email, password=password, **extra_fields)
user.set_password(password)
user.save()
return user
see if these changes make any difference.

Regards,
Amitesh 


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/38882988-f54d-4830-9cbd-9b9ad49349c0n%40googlegroups.com.

Manuel Buri

unread,
Apr 4, 2021, 6:03:37 AM4/4/21
to django...@googlegroups.com
Hi Amitesh,

unfortunately, nothing changed... I mean it is just weird that I am having now those different tables in auth and users app (see picture).
I can add and manage groups for auth_group but I would love to manage those via users_account_groups.... any ideas?

Also currently my groups are stored in auth_group while the permission and user allocation is stored in users_account_groups as well as users_account_user_permissions...
Is this normal?

Screenshot 2021-04-04 at 10.26.11.png

Best wishes,

Manuel





You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/kWu37UERkvw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1261334457.2585167.1617512381261%40mail.yahoo.com.

Ángeles

unread,
Apr 9, 2021, 7:47:50 PM4/9/21
to Django users
For creating Custom Users I have found util the tutorial: Creating a Custom User Model in Django of  Michael Herman
Reply all
Reply to author
Forward
0 new messages