首页 > 解决方案 > 从多个字段中删除重复项并在 Django 中返回查询集

问题描述

我有通过上下文发送的查询,如下所示:

context = { 
    'locations' = TravelData.objects.exclude(Q(from_lat__isnull=True) | Q(from_long__isnull=True) | Q(to_lat__isnull=True) | Q(to_long__isnull=True)).values('from_lat', 'from_long', 'to_lat', 'to_long')
}

from_lat这里 ( and from_long) 和 ( to_latand )有多个值to_long。我只想添加一次类似的值,即,我必须一次检查from值和to值,如果存在则排除。

在前端,我在地图上渲染这些位置,该点的坐标将是

[from_long, from_lat] or [to_long, to_lat]

如何更改上述查询以获得最有效的查询集?

编辑:

这是查询集的一个示例:

<QuerySet [{'to_lat': '52.92732', 'to_long': '77.63575', 'from_lat': '52.92415', 'from_long': '77.67229'}, {'to_lat': '52.92768', 'to_long': '77.62664', 'from_lat': '52.96691', 'from_long': '77.74935'}, {'to_lat': '53.047926', 'to_long': '77.597766', 'from_lat': '52.937222', 'from_long': '77.626915'}, {'to_lat': '52.97143', 'to_long': '77.63914', 'from_lat': '52.98999', 'from_long': '77.55332'}, {'to_lat': '52.92732', 'to_long': '77.63575', 'from_lat': '52.92415', 'from_long': '77.67229'}, {'to_lat': '52.92768', 'to_long': '77.62664', 'from_lat': '52.96691', 'from_long': '77.74935'}, {'to_lat': '53.047926', 'to_long': '77.597766', 'from_lat': '52.937222', 'from_long': '77.626915'}, {'to_lat': '52.97143', 'to_long': '77.63914', 'from_lat': '52.98999', 'from_long': '77.55332'}, {'to_lat': '52.97143', 'to_long': '77.63914', 'from_lat': '58.98999', 'from_long': '80.55332'}]>

<script>html文件的标签中,一部分代码是这样的:

var locations_object = {
    type: 'FeatureCollection',
    features: [
        {% for i in locations %}
            {
                type: 'Feature',
                geometry: {
                    type: 'geojson',
                    coordinates: ["{{i.from_long}}", "{{i.from_lat}}"]
                },
                properties: {
                    title: 'from_locations',
                    description: 'from_locations'
                }
            },
            {
                type: 'Feature',
                geometry: {
                    type: 'geojson',
                    coordinates: ["{{i.to_long}}", "{{i.to_lat}}"]
                },
                properties: {
                    title: 'to_locations',
                    description: 'to_locations'
                }
            },
        {% endfor %}
    ]
};

因此,这里具有相同坐标值的位置在数据库中重复多次,如locations上下文变量所示。但是在渲染地图时,我只需要它一次。因此,在上下文中的查询中,如果可能的话,我如何才能将[to_lat, to_long]配对和[from_lat, from_long]配对一次?例如:在前四个元素locationscontext再次重复。所以我必须只考虑一次。然后在第九个元素中,to_latandto_long被重复,因为它已经存在于前一个元素中。所以我必须排除它,但它from_latfrom_long没有出现在前面的元素中。我必须在第九个要素中只考虑这一点。同样适用于所有条件。

标签: django

解决方案


听起来您只需要根据这 4 个字段查找不同的 TravelData 对象。您可以简单地添加.distinct(*fields)到查询中以获取您要查找的内容。

由于您想要对的不同值,我建议对查询中的一对做不同的,然后您可以从值创建集合

latlong = TravelData.objects.exclude(
        Q(from_lat__isnull=True) | 
        Q(from_long__isnull=True) | 
        Q(to_lat__isnull=True) | 
        Q(to_long__isnull=True)
    ).distinct('from_lat', 'from_long'
).values('from_lat', 'from_long', 'to_lat', 'to_long')

from_pairs = {(x['from_lat'], x['from_long']) for x in latlong}
to_pairs = {(x['to_lat'], x['to_long']) for x in latlong}

all_possible_from_to_pairs = {(from, to) for from in from_pairs for to in to_pairs}

https://docs.djangoproject.com/en/3.0/ref/models/querysets/#django.db.models.query.QuerySet.distinct

编辑:在更清楚地理解问题后更新代码。


推荐阅读