本文整理匯總了Python中django.contrib.admin.utils.lookup_needs_distinct方法的典型用法代碼示例。如果您正苦於以下問題:Python utils.lookup_needs_distinct方法的具體用法?Python utils.lookup_needs_distinct怎麽用?Python utils.lookup_needs_distinct使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.contrib.admin.utils
的用法示例。
在下文中一共展示了utils.lookup_needs_distinct方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: search_queryset
# 需要導入模塊: from django.contrib.admin import utils [as 別名]
# 或者: from django.contrib.admin.utils import lookup_needs_distinct [as 別名]
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