开发者

Django - User account with multiple identities

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 Id开发者_开发百科entity 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)?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜