根据公共字段组合两个Django Queryset

时间:2020-05-14 16:23:58

标签: django python-3.x orm django-orm

我有两个查询集(实际上是字典列表),如:

q1 = M1.objects.filter(id=pk).values('p_id', 'q1_quantity')
# q1: <Queryset[{'p_id': 2, 'q1_quantity': 4}, {'p_id': 3, 'q1_quantity': 5}]>

q2 = M2.objects.filter(p_id__in=[q1[x]['p_id'] for x in range(len(q1))]).values('p_id', 'q2_quantity')
# q2: <Queryset[{'p_id': 2, 'q2_quantity': 2}, {'p_id': 2, 'q2_quantity': 5}, {'p_id': 3, 'q2_quantity': 1}, {'p_id': 3, 'q2_quantity': 7}]>

q1具有不同的键:值对,而q2具有重复的键。

1)我想通过公共p_id对q2的所有值求和,以使q2变为:

# q2: <Queryset[{'p_id': 2, 'q2_quantity': 7}, {'p_id': 3, 'q2_quantity': 8}]>

2)然后,根据常见的p_id将q1和q2合并到q3中,例如:

q3 = ?
# q3: <Queryset[{'p_id': 2, 'q1_quantity': 4, 'q2_quantity': 7}, {'p_id': 3, 'q1_quantity': 5, 'q2_quantity': 8}]>

我调查了union()。但是不知道如何对查询集(q2)求和然后将其与q1合并。

有人可以帮我吗?

2 个答案:

答案 0 :(得分:0)

问题是您实现的是效率低下的模型,拥有2个单独的具有重复字段的模型将迫使您进行2个查询。您可能需要考虑将它们全部合并在一个模型中,或者thresh = 0.75 ref = pd.DataFrame(easy_df['c'].tolist()) out = easy_df[ref.gt(thresh).any(1)].assign(d=ref.idxmax(1)) print(out) a b c d 0 1 9 [0.9, 0.3, 0.1] 0 1 2 8 [0.8, 0.7, 0.2] 0 4 5 5 [0.5, 0.9, 0.5] 1 5 6 4 [0.4, 0.8, 0.6] 1 7 8 2 [0.2, 0.1, 0.8] 2 8 9 1 [0.1, 0.4, 0.9] 2 模型扩展M2

models.py

M1

然后在您的views.py

class M(models.Model):
    p_id = #Your Field...
    q1_quantity = #Your Field...
    q2_quantity = #Your Field...

潜在问题::在您发布的代码中,注释部分显示了多个对象的查询集,并且q = M.objects.filter(id=pk).values('p_id', 'q1_quantity', 'q2_quantity') 作为主键应该是唯一的,因此应返回唯一的对象queryset

答案 1 :(得分:0)

1)我想通过公共p_id对q2的所有值求和,以使q2变为:

photo = PhotoImage(file = "pictures/ball.png")
resized_photo=photo.subsample(x=int(screen_width / (167 * scale_factor_square)),
                                                    y=int(screen_width / (167 * scale_factor_square)))

New_Championship_button = Button(
            fenetre_accueil, bg='black', image=resized_photo, activebackground="white", bd=0, highlightthickness = 0, command=lambda: create_new_championship())
    New_Championship_button.configure(height=int(screen_height/28*1.77), width=int(screen_width/28))

使用过的itertools.combinations:

# q2: <Queryset[{'p_id': 2, 'q2_quantity': 7}, {'p_id': 3, 'q2_quantity': 8}]>

2)然后,根据常见的p_id将q1和q2合并到q3中,例如:

from itertools import combinations

compare = []
for a, b in combinations(q2, 2):
    if a['p_id'] == b ['p_id']:
        a['q2_quantity'] += b['q2_quantity']
        if len(compare) <= 0:
            compare.append(a)
        else:
            [compare[d]['q2_quantity'] for d in range(len(compare)) if a['p_id'] == compare[d]['p_id']]

    else:
        if len(compare) <= 0:
            compare.append(a)
            compare.append(b)
        else:
            if any([a['p_id'] == compare[d]['p_id'] for d in range(len(compare))]):
                pass
            else:
                compare.append(a)
            if any([b['p_id'] == compare[d]['p_id'] for d in range(len(compare))]):
                pass
            else:
                compare.append(b)

根据此SO post

q3 = ?
# q3: <Queryset[{'p_id': 2, 'q1_quantity': 4, 'q2_quantity': 7}, {'p_id': 3, 'q1_quantity': 5, 'q2_quantity': 8}]>