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
On Saturday, 3 April, 2021, 10:24:52 pm IST, Manuel Buri
<[email protected]> wrote:
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 modelsfrom django.contrib.auth.models import
AbstractBaseUser, BaseUserManager, PermissionsMixinfrom
django.contrib.auth.models import Userfrom 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
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/38882988-f54d-4830-9cbd-9b9ad49349c0n%40googlegroups.com.
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/1261334457.2585167.1617512381261%40mail.yahoo.com.