I’ve been trying to create the models for my application but I run to this issue i
I want to create a user with multiple roles simultaneously. when I create an account in the user model with the role of teacher and admin .the instances are not created in the Teacher and Admin models
models.py*
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.db.models.fields.related import ForeignKey
from django.utils.translation import gettext as _
# Create your models here.
class Role(models.Model):
STUDENT = 1
TEACHER = 2
SECRETARY = 3
SUPERVISOR = 4
ADMIN = 5
ROLE_CHOICES = (
(STUDENT, 'student'),
(TEACHER, 'teacher'),
(SECRETARY, 'secretary'),
(SUPERVISOR, 'supervisor'),
(ADMIN, 'admin'),
)
id = models.PositiveSmallIntegerField(choices=ROLE_CHOICES, primary_key=True)
def __str__(self):
return self.get_id_display()
class User(AbstractUser):
roles = models.ManyToManyField(Role)
username = models.CharField(max_length = 50, blank = True, null = True, unique = True)
email = models.EmailField(_('email address'), unique = True)
native_name = models.CharField(max_length = 5)
phone_no = models.CharField(max_length = 10)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username', 'first_name', 'last_name']
def __str__(self):
return "{}".format(self.email)
class Etudiant(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
address = models.CharField(max_length = 50, blank = True, null = True)
class Teacher(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
profession = models.CharField(max_length = 50, blank = True, null = True)
class Admin(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
maman = models.CharField(max_length = 50, blank = True, null = True)
admin.py
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from accounts.models import Etudiant, Teacher, User,Role,Admin
from django.contrib.auth.forms import UserChangeForm
admin.site.site_header = 'Scientific Journal Admin'
admin.site.site_title = 'Scientific Journal '
admin.site.site_url = 'http://Scientific+Jounal.com/'
admin.site.index_title = 'Scientific Journal administration'
admin.empty_value_display = '**Empty**'
class UserAdmin(BaseUserAdmin):
form = UserChangeForm
fieldsets = (
(None, {'fields': ('email', 'password', )}),
(_('Personal info'), {'fields': ('first_name', 'last_name' , 'roles')}),
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
'groups', 'user_permissions')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
(_('user_info'), {'fields': ('native_name', 'phone_no')}),
)
add_fieldsets = (
(None, {
'classes': ('wide', ),
'fields': ('email', 'password1', 'password2'),
}),
)
list_display = ['email', 'first_name', 'last_name', 'is_staff', "native_name", "phone_no"]
search_fields = ('email', 'first_name', 'last_name')
ordering = ('email', )
admin.site.register(User, UserAdmin)
admin.site.register(Role)
admin.site.register(Etudiant)
admin.site.register(Admin)
admin.site.register(Teacher)