使用Django ORM计算组合(CROSS JOIN)

时间:2016-01-22 15:17:14

标签: django django-queryset django-orm

我有三个相关的模型:流程,因素和水平。流程与因子有多对多关系,因子有一个或多个级别。我试图计算与流程相关的所有级别组合。使用Python的itertools作为模型方法可以直接实现,但执行速度有点慢,所以我试图弄清楚如何使用Django ORM在SQL中执行此计算。

型号:

class Process(models.Model):
    factors = models.ManyToManyField(Factor, blank = True)

class Factor(models.Model):
    ...

class Level(models.Model):
    factor = models.ForeignKey(Factor, on_delete=models.CASCADE)

示例:进程正在运行涉及三个因素(距离爬升表面),每个因素由一个级别数量(长/短平/丘陵道路/混合/路径)。在SQL中计算组合将涉及通过首先确定涉及多少因子(本例中为3)并多次执行所有级别的CROSS JOIN来构建查询。

在SQL中,这可以这样完成:

WITH foo AS
    (SELECT * FROM Level
     WHERE Level.factor_id IN
        (SELECT ProcessFactors.factor_id FROM ProcessFactors WHERE process_id = 1)
    )
SELECT a1.*, a2.*, a3.*
    FROM foo a1
    CROSS JOIN foo a2
    CROSS JOIN foo a3
WHERE (a1.factor_id < a2.factor_id) AND (a2.factor_id < a3.factor_id)

a1.name | a2.name | a3.name
--------------------------
Long    | Flat    | Road
Long    | Flat    | Mixed
Long    | Flat    | Trail
Long    | Hilly   | Road
Long    | Hilly   | Mixed
Long    | Hilly   | Trail
Short   | Flat    | Road
Short   | Flat    | Mixed
Short   | Flat    | Trail
Short   | Hilly   | Road
Short   | Hilly   | Mixed
Short   | Hilly   | Trail

目前,我将此作为Process模型的方法实现为:

def level_combinations(self):
    levels = []
    for factor in self.factors.all():
        levels.append(Level.objects.filter(factor = factor))

    combinations = []
    for levels in itertools.product(*levels):
        combination = {}

        combination["levels"] = levels

        combinations.append(combination)

    return combinations

这是否可以使用Django ORM或者是否足够复杂以至于应该将其作为原始查询实现以提高Python代码实现的速度?

几年前有一个关于performing CROSS JOIN in Django ORM的类似问题(看起来大概是Django v1.3)并没有引起吸引人的注意(作者只是使用Python itertools)

3 个答案:

答案 0 :(得分:2)

from itertools import groupby, product

def level_combinations(self):
    # We need order by factor_id for proper grouping
    levels = Level.objects.filter(factor__process=self).order_by('factor_id')
    # [{'name': 'Long', 'factor_id': 1, ...},
    #  {'name': 'Short', 'factor_id': 1, ...},
    #  {'name': 'Flat', 'factor_id': 2, ...},
    #  {'name': 'Hilly', 'factor_id': 2, ...}]

    groups = [list(group) for _, group in groupby(levels, lambda l: l.factor_id)]
    # [[{'name': 'Long', 'factor_id': 1, ...},
    #   {'name': 'Short', 'factor_id': 1, ...}],
    #  [{'name': 'Flat', 'factor_id': 2, ...},
    #   {'name': 'Hilly', 'factor_id': 2, ...}]]

    # Note: don't forget, that product is iterator/generator, not list
    return product(*groups)

如果订单无关紧要,那么:

def level_combinations(self):
    levels = Level.objects.filter(factor__process=self)
    groups = {}
    for level in levels:
        groups.setdefault(level.factor_id, []).append(level)
    return product(*groups.values())

答案 1 :(得分:1)

如果我理解正确,你可以尝试:

for process in Process.objects.all():
    # get all levels for current process
    levels = Level.objects.filter(factor__in=process.factors.all())

答案 2 :(得分:0)

几年后,这种解决方法没有实际使用CROSS JOIN,但它确实单个中产生了所需的结果em> 查询。

第 1 步:将 cross 字段添加到您的 Factor 模型

class Factor(models.Model):
    cross = models.ForeignKey(
        to='self', on_delete=models.CASCADE, null=True, blank=True)
    ...

第 2 步:使用新的 'Climb' 字段将 'Surface' 链接到 'Distance',并将 'Climb' 链接到 Factor.cross

第三步:查询如下

Level.objects.filter(factor__name='Distance').values_list(
    'name', 'factor__cross__level__name', 'factor__cross__cross__level__name')

结果:

('Long', 'Flat', 'Road')
('Long', 'Flat', 'Mixed')
('Long', 'Flat', 'Trail')
('Long', 'Hilly', 'Road')
('Long', 'Hilly', 'Mixed')
('Long', 'Hilly', 'Trail')
('Short', 'Flat', 'Road')
('Short', 'Flat', 'Mixed')
('Short', 'Flat', 'Trail')
('Short', 'Hilly', 'Road')
('Short', 'Hilly', 'Mixed')
('Short', 'Hilly', 'Trail')

这是一个简化的例子。为了使其更通用,您可以添加一个带有两个外键的新 Factor.cross 模型,而不是添加 CrossedFactors 字段到 Factor。然后可以使用该模型来定义各种实验设计。