Python django.contrib.admin.utils.lookup_needs_distinct() Examples

The following are 1 code examples of django.contrib.admin.utils.lookup_needs_distinct(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.contrib.admin.utils , or try the search function .
Example #1
Source File: search.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def search_queryset(self, queryset, search_term, **kwargs):
        if not search_term or not self.search_fields:
            return queryset

        orm_lookups = ['%s__icontains' % str(search_field)
                       for search_field in self.search_fields]
        for bit in search_term.split():
            or_queries = [Q(**{orm_lookup: bit})
                          for orm_lookup in orm_lookups]
            queryset = queryset.filter(reduce(operator.or_, or_queries))
        opts = queryset.model._meta
        for search_spec in orm_lookups:
            if lookup_needs_distinct(opts, search_spec):
                return queryset.distinct()
        return queryset