views:

90

answers:

2

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

+2  A: 

Why not keep track of which profile is being used with the session instead of the database. It doesn't make sense to store state in the database, that's what sessions are for.

apphacker
You make a very good point. So, between the three IdentityType models, you would need some kind of unique ID to store as a string in the session, right? Or, would it be less difficult to just store an Identity type and an ID that is unique only to that type? Sorry, I'm still learning...
Scott Willman
Well sessions are already unique to users, just store the identityType's id?
apphacker
+1  A: 

It might be nice to have the UserProfile to keep track of which Identity is currently being used.

Assuming that each Identity is a different model you could do one of two things to make this happen.

  • You could have a common parent model class that each identity class inherits from, and you could add a foreign key on the Profile to the parent identity class.

  • Alternatively if inheriting doesn't make practical sense, you could use generic relations.

digitaldreamer
Thanks, but I think that will add an extra layer of complexity that I can't yet keep a handle on...
Scott Willman