2

Hi All!

I have a model structured something like this

class BaseUser(models.Model):
    user_data = models.ForeignKey(settings.AUTH_USER_MODEL) #External Auth User Model

class Teacher(BaseUser):
    pass

class Student(BaseUser):
    pass

And I am adding all the models to Django admin like so.

for model in get_models(get_app('MyApp')):
    admin.site.register(model)

In the admin panel, I can create/view a list of BaseUser, Teacher, and Student. Where Teacher/Student are subsets of BaseUser.

The Question

When a new user is created, it is automatically a BaseUser. Is there a way to change the class of an user from BaseUser to Teacher or Student in the admin panel?

1 Answer 1

3

No there's no builtin way to do it in admin. You either have to code it yourself or if you want to create Teacher do it from it's admin create view.

It's not only about python class of model but also database representation. For each model that use concrete inheritance, special table is created that holds additional fields for the model subclass and automatically created OneToOneField to parent. Details here.

Edit: Try to specify parent link field which may be manageable from admin and so it'll allow you to create e.g. new Teacher linked with already existing BaseUser.

Edit: Specifying parent link will not help as well because that field will not appear in admin.

Not the answer you're looking for? Browse other questions tagged or ask your own question.