简体   繁体   中英

Extending the existing User model in Django

Let's say I have an app that has 3 kinds of user

  • Employee with fields like work_number , department etc.
  • Member with fields like phone_number , .....
  • Agent with fields like company_name ,.....

They're different.

I write models like this:

class Employee(models.Model):
    user = models.OneToOneField(User)
    department = models.CharField(max_length=100)
    .......

class Member(models.Model):
    user = models.OneToOneField(User)
    .......

class Agent(models.Model):
    user = models.OneToOneField(User)
    .......

Now I can :

u = User.objects.get(username='xxxx')
u.employee // it works...

but, ONE user has THREE profiles (employee,member,agent) !!!

It's not matches my business, the business role is ONE user could be and must be only one kind of 3 profiles.

What's the best way to solve this?

Why not have a single profile with a role associated (even if certain fields will never be filled for a specific role)?

You can handle the validation through forms and instead having 3 different models you would have 3 different forms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM