Ticket #2559: django-search_fields-operators_pre_magic_removal.diff

File django-search_fields-operators_pre_magic_removal.diff, 1.4 KB (added by eric@…, 18 years ago)

The same patch back-ported to the pre-magic-removal tag (changeset r2808)

  • django/contrib/admin/views/main.py

     
    218218                        lookup_params['select_related'] = True
    219219                        break
    220220        lookup_params['order_by'] = ((order_type == 'desc' and '-' or '') + lookup_order_field,)
     221
     222        # Apply keyword searches.
     223        def construct_search(field_name):
     224            if field_name.startswith('^'):
     225                return "%s__istartswith" % field_name[1:]
     226            elif field_name.startswith('='):
     227                return "%s__iexact" % field_name[1:]
     228            elif field_name.startswith('@'):
     229                return "%s__search" % field_name[1:]
     230            else:
     231                return "%s__icontains" % field_name
     232
    221233        if lookup_opts.admin.search_fields and query:
    222234            complex_queries = []
    223235            for bit in query.split():
    224236                or_queries = []
    225237                for field_name in lookup_opts.admin.search_fields:
    226                     or_queries.append(meta.Q(**{'%s__icontains' % field_name: bit}))
     238                    or_queries.append(meta.Q(**{construct_search(field_name): bit}))
    227239                complex_queries.append(reduce(operator.or_, or_queries))
    228240            lookup_params['complex'] = reduce(operator.and_, complex_queries)
    229241        if opts.one_to_one_field:
Back to Top