如何使用Django ORM创建多个内部联接?

时间:2018-01-19 01:04:29

标签: django orm inner-join

我努力寻找解决以下困境的文档和示例。我有两个表,结果(包含比赛结果)和照片(每张照片包含该比赛中的跑步者)。两个表都包含entrant_numbers,即比赛中跑步者的数量和照片中跑步者的数量。在SQL中我加入了它们,以获得每个跑步者的照片捕获总数。

SELECT * FROM Photo p 
INNER JOIN Result r ON p.entrant_number=r.entrant_number AND p.race_year=r.race_year
WHERE r.user_id=123

我的模型结构如此;

class Photo(models.Model):
    photo_id = models.AutoField(primary_key=True)
    race_id = models.ForeignKey('Race')
    url_pre_string = models.CharField("URL pre-string", max_length=255)
    filename = models.CharField("Filename", max_length=100)
    extension = models.CharField("File extensions", max_length=4)
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)


class Race(models.Model):
    race_id = models.AutoField(primary_key=True)
    race_year = models.PositiveIntegerField("Race year", null=False, blank=False)
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)


class Result(models.Model):
    result_id = models.AutoField(primary_key=True)
    race_id = models.ForeignKey('Race')
    position = models.PositiveIntegerField("Position", default=0)
    entrant_number = models.PositiveIntegerField("Entrant number", default=0, null=True, blank=True)
    user_id = models.ForeignKey(User, null=True)
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)

我尝试过的最新查询是

photo_captures = Photo.objects.filter(result__user_id_id=current_user.id)

我希望这会遍历结果并过滤当前用户ID。

1 个答案:

答案 0 :(得分:0)

您可以通过执行原始SQL查询来连接两个表。

您可以查看如何执行此操作here

from django.db import connection
def my_custom_sql(self):
    cursor = connection.cursor()    
    cursor.execute("select *
                from myapp_photo p
                inner join myapp_result r ON 
                p.entrant_number=r.entrant_number AND 
                p.race_year=r.race_year
                WHERE r.user_id=123")
    row = cursor.fetchone()
    return row

替代方式: 您可以将照片模型中的外键设置为结果模型,然后您将获得不同结果的独特照片。像这样:

class Result(models.Model):
    results_id = models.AutoField(primary_key=True)
    position = models.PositiveIntegerField("Position", default=0)
    entrant_number = models.PositiveIntegerField("Entrant number", default=0, null=True, blank=True)
    race_year = models.PositiveIntegerField("Race year", default=0)
    user_id = models.ForeignKey(User, null=True)
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)

class Photo(models.Model):
    result= models.ForeignKey(Result)
    photo_id = models.AutoField(primary_key=True)
    url_pre_string = models.CharField("URL pre-string", max_length=255)
    filename = models.CharField("Filename", max_length=100)
    extension = models.CharField("File extensions", max_length=4)
    entrant_number = models.PositiveIntegerField("Entrant number", default=0, null=True, blank=True)
    race_year = models.PositiveIntegerField("Race year", default=0)
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)

然后为您可以执行的单个用户获取特定年份结果的照片

photos = Photo.objects.filter(entrant_number = result__entrant_number,
                              race_year = result__race_year,
                              result__user_id = 123)

当然我没有测试过这个,但这应该可以解决问题。让我知道这是否有效或者您是否需要任何帮助。