如何在djongo中引用嵌套模型als外键

时间:2018-10-13 14:56:46

标签: django mongodb djongo

我正在尝试Djongo: 是否有可能将另一个模型中的djongo嵌套模型引用为外键? 或者您将如何处理djongo中的这种结构? 诊所1:n病房- Ward 1:n Cases(案例仍应保存在自己的收藏夹中。)

class Clinic(models.Model):
...
    wards = models.ArrayModelField(model_container=Ward, null=True,)
...

class Case(models.Model):

 ward = models.ForeignKey(
        'clinic.ward', on_delete=models.PROTECT)

1 个答案:

答案 0 :(得分:0)

我会这样

class Clinic(models.Model):
    ...

class Ward(models.Model):
    clinic = models.ForeignKey('Clinic', related_name='wards', on_delete=models.PROTECT)

class Case(models.Model):
    ward = models.ForeignKey('Ward', related_name='cases' on_delete=models.PROTECT)