django_filters自定义搜索

我的queryset 大概是这样:

第一条 content:'

hellword

123

' 第二条 content:'

nihao

hello

' 第三条 content:'

hi

456

'

api接口访问:http://x.x.x.x:/api/v1/shuyu/?content=hi

搜索到第三条:content:'

hi

456

'

api接口访问:http://x.x.x.x:/api/v1/shuyu/?content=he 搜索到第一&二条: content:'

hi

456

' content:'

nihao

hello

'

FilterSet设置如下:

class ShuYuFilter(django_filters.rest_framework.FilterSet):

#content = django_filters.CharFilter(field_name='content', lookup_expr='icontains',)
content = django_filters.CharFilter(field_name="content", method='my_filter',)
def my_filter(self,queryset, field_name, value):
    return queryset.filter(content__icontains=value)

class Meta:
    model = ShuYu
    fields = ['content']

我想实现只匹配 第一段

xxx

内的queryset http://x.x.x.x:/api/v1/shuyu/?content=he

只返回第一条 content:'

hellword

123

'

评论 0