djangodjango-modelsdjango-usersdjango-profiles

What is better way to have multiple type of member profiles in django


I see another question on stackoverflow.com whose title seems similar but that doesnot fulfil my requirements and my users are very different so only different roles will may be not work well. I have scenario of job portal where one type of user is a company which have different attributes different functionality while other is candidate who can show his profile and resume e.t.c., they will have different URLs. So they are totally different but common thing is that they are both Registered users. They will use login forms, they will have change password and I intend to use User class for that purpose.

Actual problem I am facing is about UserProfile class usage. UserProfile is use for profiles but in my case these two users are totally different and need many different things in profile. Also I will may be add more user types in system in future. While in django, we tell about profile is by adding this single model in settings.py

AUTH_PROFILE_MODULE = ‘accounts.userprofile’

So is there a way to do this by using some sort of inheritance or abstract class in django or some other way so that I can get intended functionality and can use django Profiles?


Solution

  • EDIT: Ok, upon further inspection, my previous answer clearly will not work.

    I had advocated abstracting the UserProfile class. However, if you do that, you cannot instantiate it, so you're back to square one.

    However, you can use Multi-table inheritance to achieve what you want. See this in the docs and this Quora thread that provided the inspiration.

    The code I posted before remains largely unchanged, save for the exclusion of the Meta sub-class and the abstract variable.

    class UserProfile(models.Model):
        # Some Stuff
    
    class CompanyProfile(UserProfile):
        # Some more stuff
    
    class CandidateProfile(UserProfile):
        # Even more stuff