2013-12-23 73 views
0

我想用flask-peewee運行下面的查詢,如何通過URL在flask-peewee中過濾查詢?

merchant_name LIKE '%海邊'

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

這裏是下面的URL返回JSON,我需要過濾merhcnat_name http://www.XXXXX.com/api/merchant_details/

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" 
} 
    ] 
} 

回答

2

您應該編碼的URL是這樣的:

?merchant_name__like=seaside%25 

您可以通過urllib.urlencode({'merchant_name__like': 'seaside%'})獲得編碼的查詢字符串,?merchant_name__like=seaside意味着SQL LIKE seaside

+0

感謝您的回答,我們如何避免小寫和大寫?海邊%25有結果,但海邊%25沒有任何結果 –

+1

您可以改爲使用ilike。 – iMom0

+0

太棒了!謝謝 –

-1

您可以使用sqlalchemy-elasticquery,讓你做搜索是這樣的:

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

和其他自定義過濾器。