gpt4 book ai didi

python - Q 对象的不同值

转载 作者:太空宇宙 更新时间:2023-11-04 07:34:27 25 4
gpt4 key购买 nike

我在 Py3 上的 Django 项目 1.10.1 上有这个查询:

Event.objects.filter(Q(subject=topic.id) | Q(object=topic.id) | Q(place=topic.id))

如何防止获得两个相同的 Event 记录?

提前谢谢你。

最佳答案

使用 distinct运算符(operator):

Event.objects.filter(Q(subject=topic.id) | Q(object=topic.id) | Q(place=topic.id)).distinct()

来自文档:

By default, a QuerySet will not eliminate duplicate rows. In practice, this is rarely a problem, because simple queries such as Blog.objects.all() don’t introduce the possibility of duplicate result rows. However, if your query spans multiple tables, it’s possible to get duplicate results when a QuerySet is evaluated. That’s when you’d use distinct().

在实现之前特别注意他们的“However”子句,除非您希望实际看到重复的结果。

关于python - Q 对象的不同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39597172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com