我的问题是,如果必须输入员工表.1。必须填写用户档案的详细信息吗?
请解释这个遗产。谢谢
class Profile(models.Model):
user = models.ForeignKey(User, unique=True)
created_date = models.DateTimeField(auto_now_add=True)
emp_first_name = models.CharField(max_length=255)
emp_last_name = models.CharField(max_length=255)
gender = models.CharField(max_length=2, choices = GENDER_CHOICES, null=False)
date_of_birth = models.DateField(blank=True,null=True)
address1 = models.CharField(max_length=255)
city = models.CharField(max_length=48)
state = models.CharField(max_length=48)
country = models.CharField(max_length=48)
email_id = models.EmailField(blank=True)
class Staff(UserProfile):
role = models.ManyToManyField(Role)
designation = models.CharField(max_length=48, blank=True, null=True)
education = models.CharField(max_length=255, blank=True, null=True)
答案 0 :(得分:3)
如果您查看https://docs.djangoproject.com/en/dev/topics/db/models/#multi-table-inheritance,您会看到自动一对一映射已创建。因此,将保存UserProfile的条目,并使用指向UserProfile条目的OneToOne字段保存Staff的条目。
但是,如果您希望Staff继承所有字段,我建议您在UserProfile中设置abstract = True
。这意味着Staff继承了所有这些字段,但您永远不能自己创建UserProfile(如下所述)
https://docs.djangoproject.com/en/dev/topics/db/models/#abstract-base-classes)
class Meta:
abstract = True