【发布时间】:2021-01-21 01:14:36
【问题描述】:
这是我的模型:
class Picture(models.Model):
picture_id = models.IntegerField(db_column='PictureID', primary_key=True)
gold_item =models.ForeignKey(GoldItem,db_column="GoldItemID",related_name="pictures",on_delete=models.CASCADE)
gold_item_branch = models.ForeignKey(GoldItemBranch, db_column="GoldItemBranchID", related_name="pictures", on_delete=models.CASCADE)
code = models.CharField(db_column='Code', max_length=5, blank=True, null=True)
class GoldItemBranch(models.Model):
gold_item_branch_id = models.IntegerField(db_column='GoldItemBranchID', primary_key=True)
gold_item_id = models.IntegerField(db_column='GoldItemID')
gold_item_branch_name = models.CharField(db_column='GoldItemBranchName', max_length=30, blank=True, null=True)
我需要对上述模型中的多个列执行连接操作。列是 gold_item_id 和 gold_item_branch_id
我写了 SQL 查询:
select * from Pictures
join GoldItemBranches on Pictures.GoldItemID = GoldItemBranches.GoldItemID and Pictures.GoldItemBranchID = GoldItemBranches.GoldItemBranchID
如何在 Django 查询集中执行相同的查询?
【问题讨论】:
标签: python sql django django-queryset