10

I'm wondering what people's thoughts are on joining models directly to the auth.User object vs to the user's profile model.

I'm storing some different types of models which my user are adding in my app. App users will search for other users via criteria on these models.

On the one hand, I'm thinking that if I join straight to User then I won't need to do request.user.get_profile() each time I need to grab the User's records, and it doesn't presuppose that a User always has a profile (they do in my app at the mo, but still). This leaves the profile model as just containing the user's contact details.

On the other hand, I imagine I'll most likely need values from the Profile (eg name, location) when I'm looking up these other models.

No doubt either will work, so maybe it doesn't matter, but I just wondered what other people's thoughts were.

Thanks!

Ludo.

Ludo
  • 2,739
  • 2
  • 28
  • 42

2 Answers2

3

I would also recommend creating foreign-keys to the User model. It just makes your life simpler when working with the user object in the view, for one. So, you can do things like request.user.foo_set, etc. without having to go through the profile model.

1

In general: If you want to make your apps reusable, always create foreign keys to User model.

As you already said, in most cases you will need User as well as Profile instance, so to prevent multiple database queries, use cache.

If reusability isn't relevant, create foreign key to Profile and use select_related() to get User instance with single query.

drul
  • 89
  • 3