在Django中考虑这个 Family
模型:
class Family(models.Model):
EMPLOYEE = 'Employee'
PARTNER = 'Partner'
BIRTH_PARENT_CHOICES = (
(EMPLOYEE, EMPLOYEE),
(PARTNER, PARTNER),
)
employee_user = models.OneToOneField(User, blank=True, null=True, related_name='employee_family')
partner_user = models.OneToOneField(User, blank=True, null=True, related_name='partner_family')
employee_first_name = models.CharField(max_length=255, blank=True)
employee_last_name = models.CharField(max_length=255, blank=True)
employee_email = models.CharField(max_length=255, blank=True)
employee_phone = models.CharField(max_length=255, blank=True)
partner_first_name = models.CharField(max_length=255, blank=True)
partner_last_name = models.CharField(max_length=255, blank=True)
partner_email = models.CharField(max_length=255, blank=True)
partner_phone = models.CharField(max_length=255, blank=True)
point_of_contact = models.CharField(max_length=255, choices=BIRTH_PARENT_CHOICES)
Family
由员工和合作伙伴组成,两者都具有各种属性(用户,名字,姓氏,电子邮件,电话) . 还有一个 point_of_contact
字段,它是 'Employee'
或 'Partner'
.
我希望能够做的是,在 Family
的 family
上做一些像
family.point_of_contact.phone_number
如果 family.point_of_contact == Family.EMPLOYEE
和 family.partner_phone_number
否则将解析为 family.employee_phone_number
,对于 first_name
, last_name
等也是如此 .
但是,据我所知,从https://docs.djangoproject.com/en/2.0/ref/models/fields/可以看出,无法在Django字段上定义其他属性 . 还有其他方法可以做到这一点吗?
2 回答
不,为了做到这一点,您需要创建一个单独的模型
Contact
并使用OneToOneField
从Family
加入到它,如果每个家庭只能有一个联系人,或者如果可能有超过Contact
的模型中使用ForeignKey
每个家庭一个联系人 .Django没有提供这样做的方法,但你可以用一些简单的Python来做到这一点:
这里
SimpleNamespace
有两种使用方式:作为
Family
的超类使这个例子易于测试 - 跳过它并坚持models.Model
.在
contact
属性中,保留它 .