Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top