Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #10516 -- Corrected admin search when the search_fields definit…

…ion contains multiple fields on the same base model. Thanks to Zain Memon for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10684 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b5f0aff922fe963a4df0d3449d8fe55a6acd950d 1 parent e5757f9
@freakboy3742 freakboy3742 authored
View
5 django/contrib/admin/views/main.py
@@ -229,10 +229,7 @@ def construct_search(field_name):
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()
View
107 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
+ }
+ }
+]
View
20 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 @@ class LanguageAdmin(admin.ModelAdmin):
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:
View
15 tests/regressiontests/admin_views/tests.py
@@ -890,6 +890,21 @@ def test_post_submission(self):
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',]
Please sign in to comment.
Something went wrong with that request. Please try again.