Fixed #15517 -- Fixed regression in admin search_fields option introduced in r15526. Thanks Fabian Buechler for the report and fix and Julien Phalip for adding tests.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15677 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Ramiro Morales 2011-03-01 02:04:35 +00:00
parent b1a0ad0079
commit bd3b5e8c2b
3 changed files with 33 additions and 9 deletions

View File

@ -254,12 +254,15 @@ class ChangeList(object):
return "%s__icontains" % field_name return "%s__icontains" % field_name
if self.search_fields and self.query: if self.search_fields and self.query:
orm_lookups = [construct_search(str(search_field))
for search_field in self.search_fields]
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(**{orm_lookup: bit})
for orm_lookup in orm_lookups]
qs = qs.filter(reduce(operator.or_, or_queries)) qs = qs.filter(reduce(operator.or_, or_queries))
if not use_distinct: if not use_distinct:
for search_field in self.search_fields: for search_spec in orm_lookups:
field_name = search_field.split('__', 1)[0] field_name = search_spec.split('__', 1)[0]
f = self.lookup_opts.get_field_by_name(field_name)[0] f = self.lookup_opts.get_field_by_name(field_name)[0]
if hasattr(f, 'rel') and isinstance(f.rel, models.ManyToManyRel): if hasattr(f, 'rel') and isinstance(f.rel, models.ManyToManyRel):
use_distinct = True use_distinct = True

View File

@ -258,7 +258,7 @@ class PersonAdmin(admin.ModelAdmin):
list_display = ('name', 'gender', 'alive') list_display = ('name', 'gender', 'alive')
list_editable = ('gender', 'alive') list_editable = ('gender', 'alive')
list_filter = ('gender',) list_filter = ('gender',)
search_fields = (u'name',) search_fields = ('^name',)
ordering = ["id"] ordering = ["id"]
save_as = True save_as = True
@ -445,7 +445,7 @@ class Recommendation(Title):
recommender = models.ForeignKey(Recommender) recommender = models.ForeignKey(Recommender)
class RecommendationAdmin(admin.ModelAdmin): class RecommendationAdmin(admin.ModelAdmin):
search_fields = ('titletranslation__text', 'recommender__titletranslation__text',) search_fields = ('=titletranslation__text', '=recommender__titletranslation__text',)
class Collector(models.Model): class Collector(models.Model):
name = models.CharField(max_length=100) name = models.CharField(max_length=100)

View File

@ -1470,7 +1470,7 @@ class AdminViewListEditable(TestCase):
"_save": "Save", "_save": "Save",
} }
self.client.post('/test_admin/admin/admin_views/person/?q=mauchly', data) self.client.post('/test_admin/admin/admin_views/person/?q=john', data)
self.assertEqual(Person.objects.get(name="John Mauchly").alive, False) self.assertEqual(Person.objects.get(name="John Mauchly").alive, False)
@ -1680,7 +1680,8 @@ class AdminViewListEditable(TestCase):
class AdminSearchTest(TestCase): class AdminSearchTest(TestCase):
fixtures = ['admin-views-users','multiple-child-classes'] fixtures = ['admin-views-users', 'multiple-child-classes',
'admin-views-person']
def setUp(self): def setUp(self):
self.client.login(username='super', password='secret') self.client.login(username='super', password='secret')
@ -1703,6 +1704,26 @@ class AdminSearchTest(TestCase):
self.assertContains(response, "\n1 user\n") self.assertContains(response, "\n1 user\n")
self.assertContains(response, '<input type="hidden" name="t" value="username"/>') self.assertContains(response, '<input type="hidden" name="t" value="username"/>')
def test_exact_matches(self):
response = self.client.get('/test_admin/admin/admin_views/recommendation/?q=bar')
# confirm the search returned one object
self.assertContains(response, "\n1 recommendation\n")
response = self.client.get('/test_admin/admin/admin_views/recommendation/?q=ba')
# confirm the search returned zero objects
self.assertContains(response, "\n0 recommendations\n")
def test_beginning_matches(self):
response = self.client.get('/test_admin/admin/admin_views/person/?q=Gui')
# confirm the search returned one object
self.assertContains(response, "\n1 person\n")
self.assertContains(response, "Guido")
response = self.client.get('/test_admin/admin/admin_views/person/?q=uido')
# confirm the search returned zero objects
self.assertContains(response, "\n0 persons\n")
self.assertNotContains(response, "Guido")
class AdminInheritedInlinesTest(TestCase): class AdminInheritedInlinesTest(TestCase):
fixtures = ['admin-views-users.xml',] fixtures = ['admin-views-users.xml',]