diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py index 95f4ca792c..98e5b9f6d1 100644 --- a/django/contrib/admin/views/main.py +++ b/django/contrib/admin/views/main.py @@ -229,10 +229,7 @@ class ChangeList(object): if self.search_fields and self.query: for bit in self.query.split(): or_queries = [models.Q(**{construct_search(str(field_name)): bit}) for field_name in self.search_fields] - other_qs = QuerySet(self.model) - other_qs.dup_select_related(qs) - other_qs = other_qs.filter(reduce(operator.or_, or_queries)) - qs = qs & other_qs + qs = qs.filter(reduce(operator.or_, or_queries)) for field_name in self.search_fields: if '__' in field_name: qs = qs.distinct() diff --git a/tests/regressiontests/admin_views/fixtures/multiple-child-classes.json b/tests/regressiontests/admin_views/fixtures/multiple-child-classes.json new file mode 100644 index 0000000000..5cadf4c1c5 --- /dev/null +++ b/tests/regressiontests/admin_views/fixtures/multiple-child-classes.json @@ -0,0 +1,107 @@ +[ + { + "pk": 1, + "model": "admin_views.title", + "fields": + { + } + }, + + { + "pk": 2, + "model": "admin_views.title", + "fields": + { + } + }, + + { + "pk": 3, + "model": "admin_views.title", + "fields": + { + } + }, + + { + "pk": 4, + "model": "admin_views.title", + "fields": + { + } + }, + + { + "pk": 1, + "model": "admin_views.titletranslation", + "fields": + { + "text": "Bar", + "title": 1 + } + }, + + { + "pk": 2, + "model": "admin_views.titletranslation", + "fields": + { + "text": "Foo", + "title": 2 + } + }, + + { + "pk": 3, + "model": "admin_views.titletranslation", + "fields": + { + "text": "Few", + "title": 3 + } + }, + + { + "pk": 4, + "model": "admin_views.titletranslation", + "fields": + { + "text": "Bas", + "title": 4 + } + }, + + { + "pk": 1, + "model": "admin_views.recommender", + "fields": + { + } + }, + + { + "pk": 4, + "model": "admin_views.recommender", + "fields": + { + } + }, + + { + "pk": 2, + "model": "admin_views.recommendation", + "fields": + { + "recommender": 1 + } + }, + + { + "pk": 3, + "model": "admin_views.recommendation", + "fields": + { + "recommender": 4 + } + } +] \ No newline at end of file diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py index dbadee42f9..1aeaea1b1d 100644 --- a/tests/regressiontests/admin_views/models.py +++ b/tests/regressiontests/admin_views/models.py @@ -339,6 +339,24 @@ class LanguageAdmin(admin.ModelAdmin): list_display = ['iso', 'shortlist', 'english_name', 'name'] list_editable = ['shortlist'] +# a base class for Recommender and Recommendation +class Title(models.Model): + pass + +class TitleTranslation(models.Model): + title = models.ForeignKey(Title) + text = models.CharField(max_length=100) + +class Recommender(Title): + pass + +class Recommendation(Title): + recommender = models.ForeignKey(Recommender) + +class RecommendationAdmin(admin.ModelAdmin): + search_fields = ('titletranslation__text', 'recommender__titletranslation__text',) + + admin.site.register(Article, ArticleAdmin) admin.site.register(CustomArticle, CustomArticleAdmin) admin.site.register(Section, save_as=True, inlines=[ArticleInline]) @@ -358,6 +376,8 @@ admin.site.register(Fabric, FabricAdmin) admin.site.register(Gallery, GalleryAdmin) admin.site.register(Picture, PictureAdmin) admin.site.register(Language, LanguageAdmin) +admin.site.register(Recommendation, RecommendationAdmin) +admin.site.register(Recommender) # We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2. # That way we cover all four cases: diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index a435003315..a571995d45 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -890,6 +890,21 @@ class AdminViewListEditable(TestCase): self.failUnlessEqual(Person.objects.get(name="John Mauchly").alive, False) +class AdminSearchTest(TestCase): + fixtures = ['admin-views-users','multiple-child-classes'] + + def setUp(self): + self.client.login(username='super', password='secret') + + def tearDown(self): + self.client.logout() + + def test_search_on_sibling_models(self): + "Check that a search that mentions sibling models" + response = self.client.get('/test_admin/admin/admin_views/recommendation/?q=bar') + # confirm the search returned 1 object + self.assertContains(response, "\n1 recommendation\n") + class AdminInheritedInlinesTest(TestCase): fixtures = ['admin-views-users.xml',]