0

I am facing an issue in Admin Stacked Inline. Is this DB architecture good? or is there any better approach to achieve this?

In my project I have two roles: student and teacher, and student can also become the teacher and vice versa.

Below is my base model.

class User(AbstractUser):
    class Meta:
        verbose_name =_("Student")
        verbose_name_plural = _("Students")

    ROLE = (
        ('STUDENT', 'Student'),
        ('TEACHER', 'Teacher'),
        ('STUDENT-TEACHER', 'Student-Teacher')
    )

    username = models.CharField(verbose_name=_('Username'), max_length=100, unique=True)
    email = models.EmailField(verbose_name=_('Email'),max_length=200,blank=False,null=False)
    dob = models.DateField(verbose_name=_("Date of Birth"), blank=False, null=True)
    phone_number = models.CharField(verbose_name=_("Phone Number"), blank=True, null=True, max_length = 13)
    role = models.CharField(max_length=50, choices=ROLE, blank=False, null=True)
    is_active = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    USERNAME_FIELD = 'username'

    def __str__(self):
        return self.username

and below is my teacher model

class Teacher(models.Model):
    class Meta:
        verbose_name =_("Teacher")
        verbose_name_plural = _("Teachers")

    TIMEZONES = tuple(zip(pytz.all_timezones, pytz.all_timezones))

    GENDER_CHOICES = (
        ('MALE', 'Male'),
        ('FEMALE', 'Female')
    )
    LEVEL_CHOICES = (
        ('BEGINNER', 'Beginner'),
        ('INTERMEDIATE', 'Intermediate'),
        ('ADVANCED', 'Advanced')
    )
    pke = models.BigIntegerField(primary_key=True)
    user = models.OneToOneField(User, blank=False, null=False, on_delete=models.CASCADE)
    profile_image = models.ImageField(verbose_name=_("Profile Image"), blank=True, null=True, upload_to="teacher_image", default="/dummy.png")
    gender = models.CharField(verbose_name=_("Gender"), choices=GENDER_CHOICES, blank=True, null=True, max_length=100)
    level = models.CharField(verbose_name=_("Level Taught"), choices=LEVEL_CHOICES, blank=True, null=True, max_length=100)
    country = models.CharField(verbose_name=_("Country"), max_length=30, blank=True, null=True)
    city = models.CharField(verbose_name=_("City"), max_length=50, blank=True, null=True)
    experience = models.FloatField(verbose_name=("Total Experience"), blank=True, null=True)
    time_zone = models.CharField(max_length=32, choices=TIMEZONES, blank=True, null=True, default='UTC')

main main model (user) is working as a student. Below is my code of admin.py of teacher app. I want base user model as a form, and below teacher fields in the teacher add form of django admin

class CertificateInline(admin.StackedInline):
    model = TeacherDocument
    extra = 1
    max_num = 5

class TeacherInline(admin.StackedInline):
    model = User

class TeacherAdmin(admin.ModelAdmin):
    list_display=('profile_image',  'gender', 'level', 'country')
    inlines = [CertificateInline, TeacherInline]
    # form = TeacherAdminForm
    # fields = ('username', 'email', 'dob', 'password', 'phone_number','profile_image', 'gender', 'level', 'city', 'experience', 'country', 'time_zone', 'language', 'video_file', 'music_genres','instruments', 'short_introduction', 'about', 'is_active', 'is_verified')
    # search_fields = ('username', 'email', 'dob', )
    # def get_queryset(self, request):
    #   qs = super(TeacherAdmin, self).get_queryset(request)
    #   return qs.filter(is_superuser=False)


admin.site.register(Teacher,TeacherAdmin)

and this is showing me the error:

django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:
ERRORS:
<class 'modules.teacher.admin.TeacherInline'>: (admin.E202) 'users.User' has no ForeignKey to 'teacher.Teacher'.
James Z
  • 12,209
  • 10
  • 24
  • 44

0 Answers0