我创建了一个AssetsFilter
类:
from django_filters import Filter
from django_filters import rest_framework as filters
from django_filters.fields import Lookup
from .models import Assets
class MyListFilter(Filter):
def filter(self, qs, value):
value_list = value.split(',')
return super(ListFilter, self).filter(qs, Lookup(value_list, 'in'))
class AssetsFilter(filters.FilterSet):
name = filters.CharFilter(lookup_expr='icontains', help_text=u'Filter by name')
criticality = MyListFilter(name='critical', help_text=u'Filter by_id')
class Meta:
model = Assets
fields = ['name', 'criticality ']
现在我在我的 Viewset 中使用这个过滤器,如下所示:
from .serializers import AssetSerializers
from .filters import AssetsFilter
class AssetViewSet(viewsets.ModelViewSet):
"""
This viewset automatically provides `list`, `create`, `retrieve`,
`update` and `destroy` actions.
"""
queryset = Assets.objects.all()
serializer_class = AssetSerializers
filter_backends = (DjangoFilterBackend,)
filter_class = AssetsFilter
http_method_names = ['get', 'post', 'put', 'delete']
def list(self, request):
"""
Returns a list of Asset.
"""
return super(AssetViewSet, self).list(request)
def create(self, request):
"""
Creates a new Asset.<br>
"""
return super(AssetViewSet, self).create(request)
def destroy(self, request, pk=None):
"""
Deletes a Asset.
"""
return super(AssetViewSet, self).destroy(request, pk=pk)
def retrieve(self, request, pk=None):
"""
Returns a Asset with id={id}
"""
return super(AssetViewSet, self).retrieve(request, pk=pk)
def update(self, request, pk=None, *args, **kwargs):
"""
Updates an existing Asset.<br>
"""
return super(AssetViewSet, self).update(request, pk=pk, *args, **kwargs)
创建 swagger 文档时,过滤器字段按预期出现在GET (list)
,中,GET (retrieve)
但它们也出现在POST
,和它们不应该出现的位置。PUT
PATCH
DELETE
如何禁用这些参数出现在最新版本的 django-rest-swagger 和 DRF 中?