search_fields 和 FilterSet
第一种方法
search_fields = ['code', 'name', 'short_name', 'org_type', 'trade_status', 'address', 'search'] # search 字段模糊搜索
第二种方法
from django_filters.rest_framework import FilterSet
import django_filters, json, re
class FilterIMChatToGroupMessage(FilterSet): im_session = django_filters.CharFilter(field_name="im_chat_to_group__session_id", method='filter_im_session') def filter_im_session(self, queryset, name, value): nid = self.request.GET.get("im_session") if IMChatToGroupMessage.objects.filter(from_to_user=self.request.user, im_chat_to_group__im_session__session_id=nid, is_read=True).count(): # 出于性能考虑先求总数 IMChatToGroupMessage.objects.filter(from_to_user=self.request.user, im_chat_to_group__im_session__session_id=nid, is_read=True).update( is_read=False) return queryset.filter(from_to_user=self.request.user, im_chat_to_group__im_session__session_id=nid) class Meta: model = IMChatToGroupMessage fields = ["im_session"]