2

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?

Hafiz
  • 4,187
  • 12
  • 58
  • 111

1 Answers1

2

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
Todd
  • 922
  • 7
  • 19
  • so what will I set in `AUTH_PROFILE_MODULE` in settings.py so that other django apps use these profile classes as they interact with `UserProfile` class ? – Hafiz Mar 30 '12 at 19:28
  • @Hafiz You set UserProfile and then insert some logic in your views to handle the different types. But please reread my answer as I significantly edited it. the solution I originally proposed will *not* work. The new one should. – Todd Mar 30 '12 at 19:33
  • 1
    Presumably you would set AUTH_PROFILE_MODULE to UserProfile, so it would always return a UserProfile object. Not sure how that would get turned into a CompanyProfile or a CandidateProfile. But perhaps I spoke too soon. I don't know enough about how model inheritance affects what you've proposed. – alan Mar 30 '12 at 19:44
  • 1
    @alan - You can use it to return the UserProfile and then determine what type of user it is using Multi-table inheritance. Essentially `profile = request.user.get_profile()` to return the Profile object and then do True/False tests on `profile.companyprofile` and `profile.candidateprofile` to see which it is (these values are automatically populated when using this feature - you don't have to specify them yourself, @Hafiz). – Todd Mar 30 '12 at 19:48
  • @Todd. Interesting tip. I was not aware of that. I'll give it a try someday. That could come in handy. – alan Mar 30 '12 at 19:51
  • so the fields, I will specify in UserProfile will be the part of CandidateProfile table or UserProfile table? – Hafiz Mar 30 '12 at 20:59
  • Yes, the models that are inheriting it will also inherit its fields. So if you have `UserProfile.display_name`, you can call `CandidateProfile.display_name` without having to specify the field in your CandidateProfile model. – Todd Mar 30 '12 at 21:04