Django - User account with multiple identities

Posted by Scott Willman on Stack Overflow See other posts from Stack Overflow or by Scott Willman
Published on 2010-04-24T18:52:34Z Indexed on 2010/04/24 19:03 UTC
Read the original article Hit count: 202

Filed under:
|
|

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)?

© Stack Overflow or respective owner

Related posts about django

Related posts about userprofile