Ticket #15517: 15517_search_fields_regression.diff

File 15517_search_fields_regression.diff, 4.4 KB (added by Julien Phalip, 14 years ago)
  • django/contrib/admin/views/main.py

    diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py
    index 635330f..d441136 100644
    a b class ChangeList(object):  
    254254                return "%s__icontains" % field_name
    255255
    256256        if self.search_fields and self.query:
     257            constructed_fields = [construct_search(str(field_name))
     258                                  for field_name in self.search_fields]
    257259            for bit in self.query.split():
    258                 or_queries = [models.Q(**{construct_search(str(field_name)): bit}) for field_name in self.search_fields]
     260                or_queries = [models.Q(**{field_name: bit})
     261                              for field_name in constructed_fields]
    259262                qs = qs.filter(reduce(operator.or_, or_queries))
    260263            if not use_distinct:
    261                 for search_field in self.search_fields:
     264                for search_field in constructed_fields:
    262265                    field_name = search_field.split('__', 1)[0]
    263266                    f = self.lookup_opts.get_field_by_name(field_name)[0]
    264267                    if hasattr(f, 'rel') and isinstance(f.rel, models.ManyToManyRel):
  • tests/regressiontests/admin_views/models.py

    diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py
    index 9ac99cc..3b44f94 100644
    a b class PersonAdmin(admin.ModelAdmin):  
    258258    list_display = ('name', 'gender', 'alive')
    259259    list_editable = ('gender', 'alive')
    260260    list_filter = ('gender',)
    261     search_fields = (u'name',)
     261    search_fields = ('^name',)
    262262    ordering = ["id"]
    263263    save_as = True
    264264
    class Recommendation(Title):  
    445445    recommender = models.ForeignKey(Recommender)
    446446
    447447class RecommendationAdmin(admin.ModelAdmin):
    448     search_fields = ('titletranslation__text', 'recommender__titletranslation__text',)
     448    search_fields = ('=titletranslation__text', '=recommender__titletranslation__text',)
    449449
    450450class Collector(models.Model):
    451451    name = models.CharField(max_length=100)
  • tests/regressiontests/admin_views/tests.py

    diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
    index 6be07b4..545e7c4 100644
    a b class AdminViewListEditable(TestCase):  
    14701470
    14711471            "_save": "Save",
    14721472        }
    1473         self.client.post('/test_admin/admin/admin_views/person/?q=mauchly', data)
     1473        self.client.post('/test_admin/admin/admin_views/person/?q=john', data)
    14741474
    14751475        self.assertEqual(Person.objects.get(name="John Mauchly").alive, False)
    14761476
    class AdminViewListEditable(TestCase):  
    16801680
    16811681
    16821682class AdminSearchTest(TestCase):
    1683     fixtures = ['admin-views-users','multiple-child-classes']
     1683    fixtures = ['admin-views-users', 'multiple-child-classes',
     1684                'admin-views-person']
    16841685
    16851686    def setUp(self):
    16861687        self.client.login(username='super', password='secret')
    class AdminSearchTest(TestCase):  
    17021703        response = self.client.get('/test_admin/admin/auth/user/?q=joe&%s=username' % TO_FIELD_VAR)
    17031704        self.assertContains(response, "\n1 user\n")
    17041705        self.assertContains(response, '<input type="hidden" name="t" value="username"/>')
    1705 
     1706   
     1707    def test_exact_matches(self):
     1708        response = self.client.get('/test_admin/admin/admin_views/recommendation/?q=bar')
     1709        # confirm the search returned 1 object
     1710        self.assertContains(response, "\n1 recommendation\n")
     1711       
     1712        response = self.client.get('/test_admin/admin/admin_views/recommendation/?q=ba')
     1713        # confirm the search returned 1 object
     1714        self.assertContains(response, "\n0 recommendations\n")
     1715       
     1716    def test_beginning_matches(self):
     1717        response = self.client.get('/test_admin/admin/admin_views/person/?q=Gui')
     1718        # confirm the search returned 1 object
     1719        self.assertContains(response, "\n1 person\n")
     1720        self.assertContains(response, "Guido")
     1721       
     1722        response = self.client.get('/test_admin/admin/admin_views/person/?q=uido')
     1723        # confirm the search returned 1 object
     1724        self.assertContains(response, "\n0 persons\n")
     1725        self.assertNotContains(response, "Guido")
    17061726
    17071727class AdminInheritedInlinesTest(TestCase):
    17081728    fixtures = ['admin-views-users.xml',]
Back to Top