Django - User account with multiple identities
- by Scott Willman
Synopsis: Each User account has a UserProfile to hold extended info like phone numbers, addresses, etc. Then, a User account can have multiple Identities. There are multiple types of identities that hold different types of information. The structure would be like so:
User
|<-FK- UserProfile
|
|<-FK- IdentityType1
|<-FK- IdentityType1
|<-FK- IdentityType2
|<-FK- IdentityType3 (current)
|<-FK- IdentityType3
|<-FK- IdentityType3
The User account can be connected to n number of Identities of different types but can only use one Identity at a time.
Seemingly, the Django way would be to collect all of the connected identities (user.IdentityType1_set.select_related()) into a QuerySet and then check each one for some kind of 'current' field.
Question: Can anyone think of a better way to select the 'current' marked Identity than doing three DB queries (one for each IdentityType)?