I’m trying to associate an Account model with the standard django auth.User model.
The way I see it happening is to have a ForeignKey on User, pointing to Account, so I can use something like account.users.
Like so
class User(User):
account = ForeignKey(Account, related_name='users')
...
However, as we all know, the recommended way to add information to the User model is via a UserProfile.
Like so
class UserProfile(models.Model):
account = ForeignKey(Account, related_name='profiles')
user = ForeignKey(User)
...
which means that if I want to get a list of all Users in an Account, account, I need to do something like,
users = [p.user for p in account.profiles]
While this is only one line, I’m assuming that every p.user generates an additional database hit, which seems highly inefficient, as compared to if the User model were accessible through the Account model directly.
Is there a more efficient way to do this? Or am I missing something obvious here?
If I get this right you want all the User of a specific account. Given that, you should be able to do the following, or something similar.