3

I have a situation where I need to subclass a custom user model for a django 1.5 project (related question/background here: Subclassing AbstractUser in Django for two types of users )

I need an abstract user class SchoolPerson and a number of subclasses (Student, Teacher, etc) based on this. I think I've resolved that I need to have the user model in a separate DB table to the subclasses as other apps rely on AUTH_USER_MODEL, of which there can only be one.

So the way I see it, I have to options to do this: add one-to-one to the standard user model in my abstract SchoolPerson class:

class SchoolPerson(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL)
    ...
    class Meta:
        abstract = True

class Student(SchoolPerson):
    year_level = models.CharField(max_length=3)
    ...

class Teacher(SchoolPerson):
    govt_id = models.CharField(max_length=10)
    ...

Or I can make my SchoolPerson model inherit AbstractUser, but keep this class non-abstract:

class SchoolPerson(AbstractUser):
    ...
    #no Meta abstract here

class Student(SchoolPerson):
    year_level = models.CharField(max_length=3)
    ...

class Teacher(SchoolPerson):
    govt_id = models.CharField(max_length=10)
    ...

Are there any advantages to one of these over the other?

Community
  • 1
  • 1
askvictor
  • 3,621
  • 4
  • 32
  • 45
  • maybe you can achieve the same with a single user class and the permission system of django: https://docs.djangoproject.com/en/dev/topics/auth/default/#topic-authorization – Alp May 30 '13 at 01:41
  • @Alp - my different user models have different fields/properties, so I can't really do this (have edited the question to make this clearer) – askvictor May 30 '13 at 05:13

1 Answers1

0

I haven't tested this but what I expect is for your first suggestion to create two db tables: one for Student and one for Teacher each with a foreign key to the AUTH_USER_MODEL.

For the second one I expect Django to create three db tables: One for the SchoolPerson(which will be exact as the default Users table with the additional fields) and two for Student and Teacher with foreign keys to SchoolPerson.

So I think the answer depends on your requirements.

kalo
  • 408
  • 1
  • 3
  • 13