如何在 Django 查询集中进行左连接
How to make a left join in django queryset
我有两个模型,我需要做一个左连接操作
我尝试了一些解决方案,但还没有成功
型号
class SmdRepairIn(models.Model):
sum_ymd = models.DateField(blank=True, null=True)
line_nm = models.CharField(max_length=20, blank=True, null=True)
model_code = models.CharField(max_length=20, blank=True, null=True)
class SmdRepairOut(models.Model):
repair_in_id = models.CharField(max_length=20, blank=True, null=True)
repairman_out = models.CharField(max_length=30, blank=True, null=True)
SMDRepairIn.id == SmdRepairOut.repair_in_id
我要检索对应的查询结果:
select A.*, B.repairman_out
from SmdRepairIn as A
left join (select repairman_out from SmdRepairOut) B on (A.id = B.repair_in_id )
如何在django queryset中实现?
预期结果应该是:
id, sum_ymd, line_nm, model_code, repairmain_out
您应该在模型中使用 Foreignkey
:
class SmdRepairIn(models.Model):
sum_ymd = models.DateField(blank=True, null=True)
line_nm = models.CharField(max_length=20, blank=True, null=True)
model_code = models.CharField(max_length=20, blank=True, null=True)
class SmdRepairOut(models.Model):
repair_in = models.ForeignKey(SmdRepairIn, blank=True, null=True, on_delete=models.SET_NULL)
repairman_out = models.CharField(max_length=30, blank=True, null=True)
(注意:你应该重新考虑是否有一个没有SmdRepairIn的SmdRepairOut。如果没有,ForeignKey行应该是repair_in = models.ForeignKey(SmdRepairIn, on_delete=models.CASCADE)
)
这将允许您查询所需的选择。示例见此处:https://docs.djangoproject.com/en/3.2/topics/db/examples/many_to_one/
一些示例:
SmdRepairIn.objects.all().values_list('sum_ymd', 'line_nm', 'model_code', 'smdrepairout__repairman_out')
这与您的要求很接近。它使用反向查找 smdrepairout__repairman_out
.
反过来说:
SmdRepairOut.objects.all().values_list('repairman_out', 'repair_in__sum_ymd')
我有两个模型,我需要做一个左连接操作 我尝试了一些解决方案,但还没有成功
型号
class SmdRepairIn(models.Model):
sum_ymd = models.DateField(blank=True, null=True)
line_nm = models.CharField(max_length=20, blank=True, null=True)
model_code = models.CharField(max_length=20, blank=True, null=True)
class SmdRepairOut(models.Model):
repair_in_id = models.CharField(max_length=20, blank=True, null=True)
repairman_out = models.CharField(max_length=30, blank=True, null=True)
SMDRepairIn.id == SmdRepairOut.repair_in_id
我要检索对应的查询结果:
select A.*, B.repairman_out
from SmdRepairIn as A
left join (select repairman_out from SmdRepairOut) B on (A.id = B.repair_in_id )
如何在django queryset中实现?
预期结果应该是:
id, sum_ymd, line_nm, model_code, repairmain_out
您应该在模型中使用 Foreignkey
:
class SmdRepairIn(models.Model):
sum_ymd = models.DateField(blank=True, null=True)
line_nm = models.CharField(max_length=20, blank=True, null=True)
model_code = models.CharField(max_length=20, blank=True, null=True)
class SmdRepairOut(models.Model):
repair_in = models.ForeignKey(SmdRepairIn, blank=True, null=True, on_delete=models.SET_NULL)
repairman_out = models.CharField(max_length=30, blank=True, null=True)
(注意:你应该重新考虑是否有一个没有SmdRepairIn的SmdRepairOut。如果没有,ForeignKey行应该是repair_in = models.ForeignKey(SmdRepairIn, on_delete=models.CASCADE)
)
这将允许您查询所需的选择。示例见此处:https://docs.djangoproject.com/en/3.2/topics/db/examples/many_to_one/
一些示例:
SmdRepairIn.objects.all().values_list('sum_ymd', 'line_nm', 'model_code', 'smdrepairout__repairman_out')
这与您的要求很接近。它使用反向查找 smdrepairout__repairman_out
.
反过来说:
SmdRepairOut.objects.all().values_list('repairman_out', 'repair_in__sum_ymd')