在Django中使用两个外键关系限制选择

时间:2018-10-29 22:35:37

标签: django python-3.x

我有以下三个模型,一个家庭模型,一个家庭成员模型和一个职业模型。我希望职业模型仅显示属于特定家庭的成员(这与字段sno有关)。怎么会这样?

感谢您的帮助。

class Household(models.Model):
    village =  models.CharField(max_length=150, null=True, blank=True)
    household_number = models.IntegerField(blank=True, null=True)
    form_number = models.IntegerField(null=True,blank=True)
    head_of_household = models.CharField(max_length=150, null=True, blank=True)


class member(models.Model):
    sno = models.ForeignKey(Household, on_delete=models.CASCADE)
    person_number = models.IntegerField(blank=True, null=True)
    name = models.CharField(max_length=150, null=True, blank=True)
    sex_choices2 = (
        ('M','Male'),
        ('F','Female'),
        )
    sex = models.CharField(max_length=3,choices=sex_choices2,blank=True, null=True)
    age = models.CharField(max_length=30,blank=True, null=True)
    class Meta:
        unique_together = (("sno", "person_number"),)
    def __unicode__(self):
        return self.name
    def __str__(self):
        return self.name


class occupations(models.Model):
    sno = models.ForeignKey(Household,on_delete=models.CASCADE)
    person_number = models.IntegerField(blank=True, null=True)
    name = models.ForeignKey(member, blank=True, on_delete=models.SET_NULL, null=True)
    occupation = models.CharField(max_length=100, null=True, blank=True)
    class Meta:
        unique_together = (("sno", "person_number","occupation"),)

0 个答案:

没有答案