I have a Student model and I will also be creating another model called Teacher. But I want a single model called Admin to be the superuser for both of these models in their respective managers. How do I achieve this?
Here's the code I have so far:
class StudentManager(BaseUserManager):
def create_user(self, usn, email, first_name, last_name, initials, branch, **extra_fields):
if not email:
raise ValueError('Email for user must be set.')
email = self.normalize_email(email)
user = self.model(
usn=usn,
email=email,
first_name=first_name,
last_name=last_name,
initials=initials,
branch=branch,
**extra_fields)
user.set_password('password')
user.save()
return user
def create_superuser(self, name, email, **extra_fields):
if not email:
raise ValueError('Email for user must be set.')
email = self.normalize_email(email)
admin = self.model( # but self.model refers to Student, not Admin
email=email,
name=name,
**extra_fields)
admin.set_password('password')
admin.save()
return admin
class Student(AbstractBaseUser, PermissionsMixin):
usn = models.CharField(max_length=10, primary_key=True, unique=True, editable=False)
email = models.EmailField(max_length=254, unique=True)
first_name = models.CharField(max_length=50, null=False)
last_name = models.CharField(max_length=50, null=False)
initials = models.CharField(max_length=10, null=True)
branch = models.CharField(max_length=5, null=False, editable=False)
objects = StudentManager()
USERNAME_FIELD = 'usn'
EMAIL_FIELD = 'email'
REQUIRED_FIELDS = ['email', 'first_name', 'last_name', 'branch']
class Admin(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(max_length=254, unique=True)
name = models.CharField(max_length=100, null=False)
# objects = StudentManager()
USERNAME_FIELD = 'name'
EMAIL_FIELD = 'email'
REQUIRED_FIELDS = ['email', 'name']
And here's the error I get:
api.Admin.groups: (fields.E304) Reverse accessor for 'api.Admin.groups' clashes with reverse accessor for 'api.Student.groups'.
Edit: my Settings.py file:
...
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'core',
'api',
'rest_framework',
]
...
AUTH_USER_MODEL = 'api.Student