guettli
guettli

Reputation: 27041

Custom User Model per App

We upgrade an django app from 1.5 to 1.7

I am unsure if the update to Substituting a custom User model is a good idea in my case.

Since there is only one AUTH_USER_MODEL settings, only one app can be the "winner".

If you have several apps, this does not work.

What to do, if several apps want to store custom information per user?

Example:

employeeapp wants to store salary per user. And skillsapp wants to store skill_level per user.

How to solve this?

Upvotes: 1

Views: 328

Answers (1)

catavaran
catavaran

Reputation: 45585

Use the "profile" models with 1-to-1 field to the User:

employeeapp/models.py

class Employee(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL)
    salary = models.IntegerField()

skillsapp/models.py

class Handyman(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL)
    skill_level = models.IntegerField()

And then you can access these "profiles" as simple as:

user = User.objects.get(username='me')
user.employee.salary
user.handyman.skill_level

If you want to create "profile" instances automatically then use the post_save signal for User sender.

Upvotes: 1

Related Questions