如何使用Django的模型进行多重连接查询?

时间:2014-10-17 01:20:58

标签: python django django-models

我无法使用Django的模型过滤器来确定如何进行此查询。这是查询:

select 
    bs.name, 
    bf.name 
from 
    bs
join 
    bcs on bs.id_bs = bcs.bs_id
join 
    bfc on bcs.bc_id = bfc.bc_id
join 
    bf on bfc.bf_id = bf.id_bf

我如何使用Django的模型过滤器?谢谢!

编辑:

class BS(models.Model):
    id_bs = models.AutoField(primary_key=True)
    name = models.CharField(max_length=765, null=False)

class BC(models.Model):
    id_bc = models.AutoField(primary_key=True)
    name = models.CharField(max_length=765)

class BF(models.Model):
    id_bf = models.AutoField(primary_key=True)
    name = models.CharField(max_length=765)

class BCS(models.Model):
    id_bcs = models.AutoField(primary_key=True)
    bc = models.ForeignKey(BC, null=True, on_delete=models.SET_NULL)
    bs = models.ForeignKey(BS, null=True, on_delete=models.SET_NULL)

class BFC(models.Model):
    id_bfc = models.AutoField(primary_key=True)
    bf = models.ForeignKey(BF, null=True, on_delete=models.SET_NULL)
    bc = models.ForeignKey(BC, null=True, on_delete=models.SET_NULL)

0 个答案:

没有答案