Fixed #10516 -- Corrected admin search when the search_fields definition 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
This commit is contained in:
parent
e5757f9261
commit
b5f0aff922
|
@ -229,10 +229,7 @@ class ChangeList(object):
|
||||||
if self.search_fields and self.query:
|
if self.search_fields and self.query:
|
||||||
for bit in self.query.split():
|
for bit in self.query.split():
|
||||||
or_queries = [models.Q(**{construct_search(str(field_name)): bit}) for field_name in self.search_fields]
|
or_queries = [models.Q(**{construct_search(str(field_name)): bit}) for field_name in self.search_fields]
|
||||||
other_qs = QuerySet(self.model)
|
qs = qs.filter(reduce(operator.or_, or_queries))
|
||||||
other_qs.dup_select_related(qs)
|
|
||||||
other_qs = other_qs.filter(reduce(operator.or_, or_queries))
|
|
||||||
qs = qs & other_qs
|
|
||||||
for field_name in self.search_fields:
|
for field_name in self.search_fields:
|
||||||
if '__' in field_name:
|
if '__' in field_name:
|
||||||
qs = qs.distinct()
|
qs = qs.distinct()
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
|
@ -339,6 +339,24 @@ class LanguageAdmin(admin.ModelAdmin):
|
||||||
list_display = ['iso', 'shortlist', 'english_name', 'name']
|
list_display = ['iso', 'shortlist', 'english_name', 'name']
|
||||||
list_editable = ['shortlist']
|
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(Article, ArticleAdmin)
|
||||||
admin.site.register(CustomArticle, CustomArticleAdmin)
|
admin.site.register(CustomArticle, CustomArticleAdmin)
|
||||||
admin.site.register(Section, save_as=True, inlines=[ArticleInline])
|
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(Gallery, GalleryAdmin)
|
||||||
admin.site.register(Picture, PictureAdmin)
|
admin.site.register(Picture, PictureAdmin)
|
||||||
admin.site.register(Language, LanguageAdmin)
|
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.
|
# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
|
||||||
# That way we cover all four cases:
|
# That way we cover all four cases:
|
||||||
|
|
|
@ -890,6 +890,21 @@ class AdminViewListEditable(TestCase):
|
||||||
|
|
||||||
self.failUnlessEqual(Person.objects.get(name="John Mauchly").alive, False)
|
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):
|
class AdminInheritedInlinesTest(TestCase):
|
||||||
fixtures = ['admin-views-users.xml',]
|
fixtures = ['admin-views-users.xml',]
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue