简体   繁体   English

使用通配符过滤请求

[英]filter request get using wildcard

I am trying to create a function in an Python API that should query an endpoint and get all items that contain a filter wildcard.我正在尝试在 Python API 中创建一个 function ,它应该查询一个端点并获取所有包含过滤器通配符的项目。

for example if I run例如,如果我跑

url = 'http://127.0.0.1:8000/api/data_product/'
response = requests.get(url, headers=headers)

I get all the items, ie我得到所有的项目,即

{
    "count": 5,
    "next": null,
    "previous": null,
    "results": [
        {
            "url": "http://127.0.0.1:8000/api/data_product/5/",
            "internal_format": false,
            "prov_report": "http://127.0.0.1:8000/api/prov-report/5/",
            "last_updated": "2022-01-28T16:59:18.173266Z",
            "name": "find/csv",
            "version": "0.0.1",
            "updated_by": "http://127.0.0.1:8000/api/users/1/",
            "object": "http://127.0.0.1:8000/api/object/311/",
            "namespace": "http://127.0.0.1:8000/api/namespace/3/",
            "external_object": null
        },
        {
            "url": "http://127.0.0.1:8000/api/data_product/4/",
            "internal_format": false,
            "prov_report": "http://127.0.0.1:8000/api/prov-report/4/",
            "last_updated": "2022-01-24T11:21:49.541879Z",
            "name": "test/csv",
            "version": "0.0.1",
            "updated_by": "http://127.0.0.1:8000/api/users/1/",
            "object": "http://127.0.0.1:8000/api/object/34/",
            "namespace": "http://127.0.0.1:8000/api/namespace/3/",
            "external_object": null
        },
       ...

I would like to find the item/s that contain a string.我想找到包含字符串的项目。 For example I would like to find that item whose name contains "find"例如,我想查找名称包含“find”的项目

I tried我试过了

'http://127.0.0.1:8000/api/data_product/?name=find'

but returns 0 items found但返回 0 个找到的项目

If I understood well, you get all items as a string, which you can filter using python:如果我理解得很好,您可以将所有项目作为一个字符串,您可以使用 python 进行过滤:

import json
name = "find"
results = json.dumps([obj for obj in json.loads(response)["results"] if obj["name"].startswith(name)], indent=4)
print(results)

Output: Output:

[
    {
        "url": "http://127.0.0.1:8000/api/data_product/5/",
        "internal_format": false,
        "prov_report": "http://127.0.0.1:8000/api/prov-report/5/",
        "last_updated": "2022-01-28T16:59:18.173266Z",
        "name": "find/csv",
        "version": "0.0.1",
        "updated_by": "http://127.0.0.1:8000/api/users/1/",
        "object": "http://127.0.0.1:8000/api/object/311/",
        "namespace": "http://127.0.0.1:8000/api/namespace/3/",
        "external_object": null
    }
]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM