简体   繁体   English

如何通过URL过滤flask-peewee中的查询?

[英]How to filter a query in flask-peewee by URL?

I'd like to run the following query by flask-peewee , 我想通过flask-peewee运行以下查询,

merchant_name LIKE 'seaside%' merchant_name喜欢'seaside%'

SELECT * 
FROM  `merchant_details` 
WHERE  `merchant_name` LIKE  'seaside%'

Here is the return JSON of the below URL and I need to filter the merhcnat_name http://www.XXXXX.com/api/merchant_details/ 这是以下URL的返回JSON,我需要过滤merhcnat_name http://www.XXXXX.com/api/merchant_details/

http://www.XXXXX.com/api/merchant_details/?merchant_name__like=seaside (This is not working) http://www.XXXXX.com/api/merchant_details/?merchant_name__like=seaside (这不起作用)

{
  "meta": {
    "model": "merchant_details",
    "next": "",
    "page": 1,
    "previous": ""
  },
  "objects": [
    {
      "merchant_name": "Seaside Grill",
      "city": "Santa Monica ",
      "zipcode": "90401",
      "long": "-118.4884742",
      "phone": "3102399844",
      "state": "CA",
      "updated_on": "2013-11-07 01:14:42",
      "lat": "34.0189893",
      "street_1": "406 Broadway",
      "street_2": "1",
      "merchant_id": "1",
      "id": "2"
    },
{
  "merchant_name": "Jack n'  Jill's",
  "city": "Santa Monica",
  "zipcode": "90401",
  "long": "-118.4937635",
  "phone": "3109873733",
  "state": "CA",
  "updated_on": "2013-11-08 08:20:29",
  "lat": "34.0173215",
  "street_1": "510 Santa Monica Blvd",
  "street_2": "1",
  "merchant_id": "48",
  "id": "32"
}
  ]
}

You should encode the url like this: 您应该这样编码网址:

?merchant_name__like=seaside%25

You can get the encoded query string by urllib.urlencode({'merchant_name__like': 'seaside%'}) , ?merchant_name__like=seaside means LIKE seaside in sql. 您可以通过urllib.urlencode({'merchant_name__like': 'seaside%'})获得编码的查询字符串, ?merchant_name__like=seaside表示在sql中LIKE seaside

You can use sqlalchemy-elasticquery that allow you make search like this: 您可以使用sqlalchemy-elasticquery来进行如下搜索:

?filters={ "merchant_nameseaside" : {"like" : "%seaside%"} }

and another custom filters. 和另一个自定义过滤器

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

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