Django过滤案例不敏感

发布于 2025-02-04 00:07:04 字数 1063 浏览 2 评论 0原文

我编写了此DJANGO视图集,该视图允许用户过滤和查询数据

from django_filters import rest_framework as filters
from rest_framework import viewsets

from API.models import Record
from API.serializers import RecordSerializer


 class    CountViewSet(viewsets.ReadOnlyModelViewSet):
    """List of all traffic count Counts"""
queryset = Record.objects.all().select_related("count")
serializer_class = RecordSerializer
filter_backends = (filters.DjangoFilterBackend,)
filterset_fields = (
    "road",
    "date",
    "count_method",
    "count_method__basic_count_method",
    "count_method__detailed_count_method",
    "location",
    "road__direction",
    "road__category",
    "road__junc_start",
    "road__junc_end",
    "road__road__name",
    "date__year",
    "location__count_point_ref",
    "road__category__name",
    "road__junc_start__name",
    "road__junc_end__name",
    "road__direction__name",
)

,我想让它使情况变得不敏感,并允许部分包含一个字符串的搜索,以便用户可以在不知道正确的“道路名称”的情况下查询:例如:

curl -x get get /count/?road__category__name = ta“ -h”接受:application/json

I wrote this Django viewset that allows users to filter and query data

from django_filters import rest_framework as filters
from rest_framework import viewsets

from API.models import Record
from API.serializers import RecordSerializer


 class    CountViewSet(viewsets.ReadOnlyModelViewSet):
    """List of all traffic count Counts"""
queryset = Record.objects.all().select_related("count")
serializer_class = RecordSerializer
filter_backends = (filters.DjangoFilterBackend,)
filterset_fields = (
    "road",
    "date",
    "count_method",
    "count_method__basic_count_method",
    "count_method__detailed_count_method",
    "location",
    "road__direction",
    "road__category",
    "road__junc_start",
    "road__junc_end",
    "road__road__name",
    "date__year",
    "location__count_point_ref",
    "road__category__name",
    "road__junc_start__name",
    "road__junc_end__name",
    "road__direction__name",
)

And I would like to make it case insensitive and allow searches that contain partially a string so that user can query without knowing the correct “road name” for example:

curl -X GET /count/?road__category__name=TA" -H "accept: application/json

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笔落惊风雨 2025-02-11 00:07:04

您可以尝试ROAD__CATEGORY__NAME__ICONTAINS如果它不起作用,则必须将其声明为自定义过滤器类中的方法,
i案例不敏感的信,
文献资源:
django icontains doc.

You can try road__category__name__icontains if it doesn't work you have to declare it as a method filed in your Custom filter class,
i letter for Case-insensitive,
Documentaion refference :
Django icontains Doc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文