簡體   English   中英

如何自定義Flask-peewee RESTfull API?

[英]How to customize Flask-peewee RESTfull api?

我是flask和python的新手,我想自定義flask-peewee restAPI以返回(緯度,經度)而不是JSON中的MerchantDetail.Address。 我需要對API對象進行更多更改,而不是直接從mysql表中獲取值。

from flask_peewee.rest import RestAPI
from geopy import geocoders
from app import app # our project's Flask app
from models import MerchantDetail

# instantiate our api wrapper
api = RestAPI(app)

# register our models so they are exposed via /api/<model>/
api.register(MerchantDetail)

# g = geocoders.GoogleV3()
# place, (lat, lng) = g.geocode(MerchantDetails.Address)

# configure the urls
api.setup()

如果我理解您的問題,則希望更改API的輸出,以使MerchantDetail包含模型上當前未包含的其他屬性(place,lat,lng)。 有兩種方法可以執行此操作:

1.)將這些字段添加到模型中,並掛接到您的應用程序中,以便在創建新的MerchanDetail時,運行地理編碼並將結果與​​模型一起存儲。

2.)使用RestResource(請參閱有關“ 定制返回的內容 ”的文檔)。 您的REST資源可以覆蓋prepare_data()方法來運行地理編碼,並將結果存儲在傳出數據中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM