django查询消除重复项
在以下查询中如何消除重复项,
d_query = Profile.objects.filter(company="12")
search_string ="Tom"
if search_string != "":
d_query = d_query.filter(Q(profiles__name__icontains=search_string) | Q(first_name__icontains=search_string)| Q(last_name__icontains=search_string))
In the following query how to eliminate the duplicates,
d_query = Profile.objects.filter(company="12")
search_string ="Tom"
if search_string != "":
d_query = d_query.filter(Q(profiles__name__icontains=search_string) | Q(first_name__icontains=search_string)| Q(last_name__icontains=search_string))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您希望避免多次返回同一记录,则可以在评估之前将
.distinct()
添加到您的查询集中Assuming you mean you want to avoid getting back the same record more than once, you can just add
.distinct()
to your queryset before evaluating it根据记录 - .distinct() 在其文档中描述了一些注意事项: http://docs.djangoproject.com/en/dev/ref/models/querysets/#distinct
For the record - .distinct() has some caveeats described in its documentation: http://docs.djangoproject.com/en/dev/ref/models/querysets/#distinct