简体   繁体   English

如何过滤数据使用等于或大于url中的条件?

[英]How to filter the data use equal or greater than condition in the url?

I can use the bellow link to filter the id=16 's data: 我可以使用波纹管链接来过滤id=16的数据:

http://localhost:8000/api/physicalservertask/list_for_home_workpanel/?id=16

this is my list api view: 这是我的列表api视图:

class PhysicalServerTaskListAPIView(ListAPIView):

    serializer_class = PhysicalServerTaskListSerializer
    permission_classes = [IsAdminUser]
    def get_queryset(self):
        query_params = self.request.query_params
        filters = {'{}__contains'.format(key): value
                   for key, value in query_params.items()
                   }

        return PhysicalServerTask.objects.filter(**filters)

I have a question, how can I query the id>= 16 's data list through the url? 我有一个问题,如何通过url查询id>= 16的数据列表?

I mean whether I can through the: 我的意思是我是否可以通过:

http://localhost:8000/api/physicalservertask/list_for_home_workpanel/?id__gte=16

to filter the data. 过滤数据。

I know I can in the ListAPIView query like this: 我知道我可以在ListAPIView查询中这样:

id_gte = self.request.query_params.copy().get('id_gte')
...
qs = PhysicalServerTask.objects.filter(**filters)
qs.filter(Q(id__gte=id__gte))

but whether there is a more convenient way to realize this? 但是否有更方便的方法来实现这一目标?

You can add a filter_fields attribute to your view like this: 您可以在视图中添加filter_fields属性,如下所示:

class PhysicalServerTaskListAPIView(ListAPIView):
    ...
    filter_fields = {
        'id': ['gte', 'lte']
    }

That will allow you to make queries such as: 这将允许您进行如下查询:

http://localhost:8000/api/physicalservertask/list_for_home_workpanel/?id__gte=16

http://localhost:8000/api/physicalservertask/list_for_home_workpanel/?id__lte=16

For that to work, you will need to install django-filter , and add the DjangoFilterBackend to your settings.py if it is not already there: 为了实现这一点,你需要安装django-filter ,并将DjangoFilterBackend添加到你的settings.py如果它还没有:

REST_FRAMEWORK = {
    ...
    'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
}

filter_fields is commonly used with a list of model fields for exact lookups. filter_fields通常与模型字段列表一起使用,以进行精确查找。 However, a dictionary can also be supplied, like in the example above, which maps model fields to other types of lookups - such as gte and lte . 但是,也可以提供字典,如上例所示,它将模型字段映射到其他类型的查找 - 例如gtelte

More information on the filter_fields attribute can be found here . 有关filter_fields属性的更多信息,请参见此处 More information on the list/dict format of filter_fields is here . 有关filter_fields的list / dict格式的更多信息,请点击此处

I would like to suggest django-url-filter 我想建议django-url-filter

From the doc, it says 它说,来自文档

# get user who joined in after 2010 as per user profile
example.com/users/?profile__joined__gt=2010-01-01<br>

Similarly, it will suit in your context 同样,它适合您的上下文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在django过滤器中做小于或等于和大于等于? - how to do less than or equal to and greater than equal to in django filter? 如何让数据大于等于之前的数据 - How to make the data greater than or equal to the previous data 如何使用python更改条件“大于或等于”的excel单元格中字符串的字体颜色? - How to change font color of string in excel cell with condition "Greater than or equal to" using python? 如何过滤大于或等于 null? - How to filter greater than or is null? 如何使 python 大于/小于或等于? - How to make a greater/less than or equal to on python? 如何使用 Python 查找等于或大于给定总和的值组合? - How To Use Python to find combinations of value with equal or greater than given sum? if条件下如何使用不等于运算符? - How to use not equal to operator in if condition? 在一个元素大于同一列但最后一行的元素的情况下,如何在DataFrame中使用applymap? - How to use applymap in a DataFrame on condition that an element is greater than the one which is at the same column but last row? python 中的“大于”或“等于”与“等于”或“大于” - “Greater than” or “equal” vs “equal” or “greater than” in python Pandas:如何过滤今天日期大于数据集中日期字段的数据集 - Pandas: how to filter data set where today's date is greater than date field in the data set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM