【问题标题】:What I am missing in order filtering against query parameter in url to work in Django Rest Framework?为了过滤 url 中的查询参数以在 Django Rest Framework 中工作,我缺少什么?
【发布时间】:2019-02-01 13:26:02
【问题描述】:

我试图在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 

更新
根据 Angela 的查询,我尝试输入 /project/api/list/?user=existing-user-uuid,但似乎 api 返回没有如下变化。

【问题讨论】:

  • 可以添加DRF,Django,django-filter的版本吗?
  • 我添加了 DRF、Django、django-filter 的版本。这个版本有问题吗?
  • 你为什么还在使用DRF 0.1.0 ??连DRF==3.7.7都可以用吗?
  • 我已经更新了 djangorestframework==3.8.2 如上所述。但问题还没有解决。你知道什么可能导致这个问题吗?
  • 我试图重现这个问题。但失败了。你能告诉我如何重现它吗?

标签: django django-rest-framework


【解决方案1】:

我尝试在 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  

【讨论】:

    猜你喜欢
    • 2021-08-23
    • 2020-09-25
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 2016-01-14
    • 2019-12-17
    • 2017-12-28
    相关资源
    最近更新 更多