django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:
ERRORS:
auth.User.groups: (fields.E304) Reverse accessor for 'auth.User.groups' clashes with reverse accessor for 'members.User.groups'.
HINT: Add or change a related_name argument to the definition for 'auth.User.groups' or 'members.User.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'auth.User.user_permissions' clashes with reverse accessor for 'members.User.user_permissions'.
HINT: Add or change a related_name argument to the definition for 'auth.User.user_permissions' or 'members.User.user_permissions'.
members.User.groups: (fields.E304) Reverse accessor for 'members.User.groups' clashes with reverse accessor for 'auth.User.groups'.
HINT: Add or change a related_name argument to the definition for 'members.User.groups' or 'auth.User.groups'.
members.User.user_permissions: (fields.E304) Reverse accessor for 'members.User.user_permissions' clashes with reverse accessor for 'auth.User.user_permissions'.
HINT: Add or change a related_name argument to the definition for 'members.User.user_permissions' or 'auth.User.user_permissions
Basically this error is saying that you need to have unqiue related_name as related name insure there is no conflict between field. It can not find settings.AUTH_USER_MODEL because you did not run makemigrations
If you want to create custom user with is_student, is_teacher , you can follow the code below cuz this is how I like to create custom users
You need to run makemigrations and migrate before you can test it! Or you can create separated app to see if this custom user suites your needs.
in utils.py
from django.db import models
import random
from django.contrib.auth.base_user import BaseUserManager
from django.utils.translation import ugettext_lazy as _
class IntegerRangeField(models.IntegerField):
"""
This model handels :
- Creating custom IntegerField with min/ max value.
- Used in Users class, employee_number.
- Gives each employee exactly 4 digits integer.
- Can be edited in future if company grows in size.
...
Attributes:
----------
min_value :
minimum number.
max_value:
maximum number.
Methods:
formfield(self, **kwargs):
- create new defaults for IntegerField then update them.
"""
def __init__(self, verbose_name=None, name=None, min_value=None, max_value=None, **kwargs):
self.min_value, self.max_value = min_value, max_value
models.IntegerField.__init__(self, verbose_name, name, **kwargs)
def formfield(self, **kwargs):
defaults = {'min_value': self.min_value, 'max_value':self.max_value}
defaults.update(kwargs)
return super(IntegerRangeField, self).formfield(**defaults)
# generate random number from 1000, 9000
def create_new_employee_number():
return random.randint(1000,9999)
class CustomUserManager(BaseUserManager):
"""
Custom user model manager where username is unique identifiers
able to add more fields to Django basic User model.
"""
def create_student(self, username, password, **extra_fields):
"""
Create and save a User with the given email and password.
"""
if not username:
raise ValueError(_('The username must be set'))
user = self.model(username=username, **extra_fields)
user.set_password(password)
user.save()
return user
def create_superuser(self, username, password, **extra_fields):
"""
Create and save a SuperUser with the given email and password.
"""
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', True)
extra_fields.setdefault('is_student', False)
extra_fields.setdefault('is_teacher', False)
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(username, password, **extra_fields)
def create_teacher(self, username, first_name, last_name, password=None):
if not username:
raise ValueError("User must have an email")
if not password:
raise ValueError("User must have a password")
if not first_name:
raise ValueError("User must have a first name")
if not last_name:
raise ValueError("User must have a last name")
user = self.model(
email=self.normalize_email(username)
)
user.first_name = first_name
user.last_name = last_name
user.set_password(password) # change password to hash
user.is_admin = False
user.is_staff = True
user.save(using=self._db)
return user
def create_staffuser(self, username, first_name, last_name, password=None):
if not username:
raise ValueError("User must have an email")
if not password:
raise ValueError("User must have a password")
if not first_name:
raise ValueError("User must have a first name")
if not last_name:
raise ValueError("User must have a last name")
user = self.model(
email=self.normalize_email(username)
)
user.first_name = first_name
user.last_name = last_name
user.set_password(password) # change password to hash
user.is_admin = False
user.is_staff = False
user.is_teacher=True
user.save(using=self._db)
return user
now in models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.db.models.signals import post_save, pre_save
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ValidationError
import datetime
from .utils import (
IntegerRangeField,
CustomUserManager,
create_new_employee_number
)
ROLES_CHOICES = (
('ST', 'Student'),
('TC', 'Teacher')
)
class User(AbstractUser):
"""
This model handels :
- follows django docs on how to customizing user mode.
- https://docs.djangoproject.com/en/3.2/topics/auth/customizing/
- Creating new users into the database.
- aumatically genearte JWT from the home/urls.py "rest_auth.registration.urls"
...
Attributes:
----------
username :
unique string field, max_length is 50
name:
user first and last name.
role:
user role two options(ST: Student, TC: Teacher)
id:
unique randomly generated integer, editable by admin.
uses custom field, more details about IntegerRangeField in utils.py
Methods:
__str__
return a string repr of the user, username.
"""
username = models.CharField(unique=True, max_length=50)
name = models.CharField(max_length=50, blank=True, default="Employee")
role = models.CharField(
max_length=50, choices=ROLES_CHOICES,
blank=False, null=False, default="ST",
error_messages = {'invalid': "Role is two choices ADMIN or EM"}
)
id= IntegerRangeField(
min_value=1000, max_value=9999, editable=True, blank=True,
unique=True, default=create_new_employee_number,
error_messages={'invalid': "employee_number is unique integer 1000-9999"}
)
objects = CustomUserManager()
def __str__(self):
return self.username