繁体   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