﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
2818	[newforms-admin] Admin searches should use distinct, if query involves joins	Andy Dustman <farcepest@…>	Adrian Holovaty	"I have a model that is searchable based on fields that are several levels of indirection away, i.e.

{{{
class Course:

    # ...
    sections = ManyToManyField(Section)

    class Admin:

        search_fields = ('name', '^sections__instructors__surname',
                         '^sections__instructors__username',
                         '^sections__call_no')
}}}

i.e. Courses refers to Sections which refers to Instructors which has a couple fields to search on. Sections may belong to more than one course, and Instructors may belong to more than one section. In the current trunk, a search on surname in this scheme can return the same Course multiple times.

To fix this, I simply changed `django.contrib.admin.views.main.ChangeList.get_query_set()` to return a `QuerySet().distinct()` all the time. This doesn't seem to break anything and it fixes the duplicate results problem. However, it's possible it may have some performance impact so you might want to make it conditional on whether or not the search is across joined tables. However, it wasn't really obvious how to do this so I went for the quick-and-(clean|dirty) solution."	enhancement	closed	contrib.admin	dev	minor	fixed		farcepest@…	Ready for checkin	1	0	0	0	0	0
