[英]Extending the existing User model in Django
Let's say I have an app that has 3 kinds of user 假设我有一个拥有3种用户的应用
work_number
, department
etc. 具有work_number
, department
等字段的员工 phone_number
, ..... 具有phone_number
,.....等字段的成员 company_name
,..... 具有诸如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. 它与我的业务不匹配,业务角色是一个用户,并且只能是3个个人资料中的一种。
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. 您可以通过表单处理验证,而拥有3个不同的模型,则可以拥有3个不同的表单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.