Django admin 实现search_fields精确查询

search_fields = (u'gift_rule_id',u'user_id', u'activity_id',)

//默认的查询集合
def get_queryset(self, request):
    return super(GiftConfirmAdmin, self).get_queryset(request).all().order_by("-id")
//根据关键字进行查询集合
def get_search_results(self, request, queryset, search_term):
    queryset, use_distinct = super(GiftConfirmAdmin, self).get_search_results(request, queryset, search_term)
    try:
        search_term_as_int = int(search_term)
        queryset &= (self.model.objects.filter(gift_rule_id=search_term_as_int) |
                     self.model.objects.filter(user_id=search_term_as_int) |
                     self.model.objects.filter(activity_id=search_term))
    except:
        pass
    return queryset, use_distinct

你可能感兴趣的:(Django admin 实现search_fields精确查询)