我试图对this page之后的url中的查询参数实施函数过滤,并且该过滤器在其他环境中也有效。
因此,我试图在其他应用程序中复制此代码。但是,即使我实现了相同的代码,也无法找到如下所示的过滤接口。
有人知道我在想什么吗?
urls.py
router=routers.SimpleRouter()
router.register(r'list',projectViewSet)
urlpatterns = [
url(r'^api/', include(router.urls)),
]
views.py
class projectViewSet(viewsets.ModelViewSet):
"""
This API returns the list of all projects with basic information to be able to filter
"""
queryset=html.objects.all()
serializer_class = projectSerializer
filter_class=projectAPIfilter
serializer.py
class projectSerializer(serializers.ModelSerializer):
area=areaSerializer(read_only=True)
unmet=unmetSerializer(read_only=True)
energy = energySer(read_only=True)
class Meta:
model = html
fields = ('pk', 'project', 'version', 'program', 'location', 'certificate', 'user', 'good', 'final','area','unmet','energy','good','final')
filters.py
class projectAPIfilter(filters.FilterSet):
user = django_filters.CharFilter(lookup_expr="iexact")
project = django_filters.CharFilter(lookup_expr="icontains")
class Meta:
model=html
fields=['project','program','location','certificate','user','good','final']
settings.py
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': (
'django_filters.rest_framework.DjangoFilterBackend',
),
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 100
}
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'corsheaders',
'rest_framework',
'rest_framework.authtoken',
'widget_tweaks',
'crispy_forms',
'rest_framework_swagger',
#'el_pagination',
'django_tables2',
'django_filters',
'heatBalance',
'project',
'ecm',
'help',
'BEAM',
'social_django',
# 'social_django_mongoengine'
]
软件包
django=2.0.4=py35_0
djangorestframework==3.8.2
django-filter==1.1.0
更新
根据安吉拉的查询,我尝试输入/ project / api / list /?user = existing-user-uuid,但api返回值似乎没有变化,如下所示。
答案 0 :(得分:0)
我尝试如下明确地在views.py中定义过滤器后端。然后问题解决如下图。仍然不知道为什么在settings.py中指定不起作用。但是如果有人遇到同样的问题,这可能是解决问题的一种方法。
views.py
class projectViewSet(viewsets.ModelViewSet):
"""
This API returns the list of all projects with basic information to be able to filter
"""
queryset=html.objects.all()
serializer_class = projectSerializer
filter_class=projectAPIfilter
filter_backends = (django_filters.rest_framework.DjangoFilterBackend,)
pagination_class = projectPagination
pagination.py
from rest_framework.pagination import PageNumberPagination
class projectPagination(PageNumberPagination):
page_size = 100