Custom Users and Profiles: General Consesus

Hi All,

I’m thinking of how to include more attributes for my user model, for example adding whether a user is a teacher or a student, whether their profile is public or private, and a few other things in the same vain.

In the past I have created a separate profile model with a one-to-one, and a signal to create the profile when the user is created.

In my current project, I’ve extended my CustomUser from AbstractUser and can add attributes to my user model as I see fit.

I was wondering however what people are leaning towards today, profile models or custom user models, or perhaps a combination of the two.

One such thought I had to model students and teachers was to have three models: one for the custom user from AbstractUser, one for the teacher model with a one-to-one to the custom user model, and a student model, again, with a one-to-one to the custom user model.

To be honest, I like the simplest approach which is just to have a single CustomUser model extended from AbstractUser, but certainly see the benefits in abstracting teacher and student from each other, as there will almost certainly be attributes which are unique to either a student or a teacher, but many attributes which will be common across the two user types.

So in short, I’d love to hear what people are doing today to solve the same sort of problem.

Cheers,

Conor

1 Like

I tend toward the position of:

  • Use profile (related) models when the only changes being made are to the data. (The preferred situation, works for us 95+% of the time.)

  • Extend the model when I need to change the behavior of the user model. (Have only needed to do this once in recent memory)

And, I would have no problems with having a base Person model having the one-to-one to User in addition to having a Student and Teacher models having one-to-ones with User as well. (A database purist might recommend associating Student and Teacher with Person instead of User as being a more accurate model, but I tend toward being more pragmatic about such things.)

Ken

I’ve always gone done the route of:

  • Creating an abstract “Profile” model with a 1-1 to the User model
  • Creating the different user types extending the Profile model
  • Creating ProxyModels for the models, in this case Student and Teacher models, so that way I can continue querying those model types specifically from the Profile model.

A few more classes than necessary, but I always prioritize readability over SLOC, personally.

Ken, Suttonium,

Thanks a million for sharing your thoughts and experience. Definitely going to play around with some of the ideas you’ve mention. Very grateful for your tips, thank you.

Cheers,

Conor

I saw this video recently on a similar topic from Daniel Feldroy.

Multiple User Types | Django

Nice one, speno. Great video. I have the Feldroy’s latest copy fo Two Scoops of Django which I can recommend, but it is not complete yet and is being updated incrementally as they finish parts of the book. Their chapter on this topic is still in # TODO

Again, cheers!