简体   繁体   English

如何在Django中合并两个查询并选择与两个不相关的表不同

[英]How do I merge two queries in django and select distinct from two unrelated tables

I'm working in Django (1.8) with a MySQL backend. 我正在使用MySQL后端在Django(1.8)中工作。 I have two unrelated tables that I want to query. 我有两个要查询的不相关表。 The results are a concatenation of two columns from each table. 结果是每个表中两列的串联。 I want to merge the results, sort them and select distinct. 我想合并结果,对其进行排序并选择不同的结果。 For the sake of example let's assume I have tableA and tableB both of which have first_name and last_name columns. 为了举例说明,假设我有tableA和tableB,它们都有first_name和last_name列。 Each cloumn heading has a reference to the table in it (eg first_name_A). 每个cloumn标题都在其中引用了该表(例如first_name_A)。 Currently I'm running a query on each table and using sorted(chain(queryA, queryB)) to put them together. 目前,我正在每个表上运行查询,并使用sorted(chain(queryA,queryB))将它们放在一起。 The problem comes when I try to reference the result in a for loop in the template. 当我尝试在模板的for循环中引用结果时,问题就来了。

Codewise I have this: 按代码我有这个:

views.py views.py

queryA = tableA.objects.order_by('first_name_A', 'last_name_A').values('first_name_A', 'last_name_A').distinct()
queryB = tableB.objects.order_by('first_name_B', 'last_name_B').values('first_name_B', 'last_name_B').distinct()

query = sorted(chain(queryA, queryB))

return render_to_response('results.html', {'queries': query})

html html

<div>
{% for query in queries %}
    <tr>
        <td>{{ query.first_name_A }} {{ query.last_name_A }}</td>
    </tr>
{% endfor %}
</div>

The above html will obviously only return the first and last names from tableA. 上面的html显然只会返回tableA的名字和姓氏。 These are sorted and distinct but no info from tableB is included. 这些已排序且不同,但不包括来自tableB的信息。 As the columns from each table have different headings and no properties alike other than the values (rather than the keys) how can I merge them, sort and select distinct combinations? 由于每个表中的列都有不同的标题,除了值(而不是键)外没有其他属性,如何合并它们,排序和选择不同的组合? Or should I just rename the columns to remove the table reference? 还是应该重命名列以删除表引用?

You can use the extra() modifier to assign alternative names. 您可以使用extra()修饰符来分配备用名称。

queryA = tableA.extra(select={'alias' : 'first_name_A'}).order_by('first_name_A').values('alias').distinct()
queryB = tableB.extra(select={'alias' : 'first_name_B'}).order_by('first_name_B').values('alias').distinct()

The django documentation discourages the use of extra but there doesn't seem to be an easy way to create aliases in a queryset. django文档不鼓励使用extra但是似乎没有一种在查询集中创建别名的简单方法。 This ticket suggest that this may change in the future. 此票证表明将来可能会发生变化。 The last comment describes an alternative method with F expressions and annotate which should work with django v1.8 最后的注释描述了带有F表达式和annotate的替代方法,该方法应与django v1.8一起使用

I've solved it but I'm not sure it's the most pythonic (nor correct) way. 我已经解决了它,但是我不确定这是最pythonic(也不正确)的方法。 If anyone has any better suggestions then please let me know. 如果有人有更好的建议,请告诉我。

views.py views.py

        queryA = tableA.objects.order_by('first_name_A','last_name_A').values('first_name_A', 'last_name_A').distinct()
        queryB = tableB.objects.order_by('first_name_B','last_name_B').values('first_name_B', 'last_name_B').distinct()


        chain_query = sorted(chain(queryA, queryB))


        valueList = []


        for q in chain_query:
            wholeName = ' '.join(q.values())
            valueList.append(wholeName)


        query = sorted(set(valueList))  

        return render_to_response('results.html', {'queries': query})

html html

    {% for query in queries %}
    <tr>
        <td>{{ query }}</td>
    </tr>
    {% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM