Вопрос

I'm newbie in flask and python, I would like to customize the flask-peewee restAPI to return the (latitude, longitude) instead of MerchantDetail.Address in JSON. I need to make more changes to API objects in place of getting values directly from mysql table.

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()
Это было полезно?

Решение

If I'm understanding your question, you want to change the output of your API so that MerchantDetail includes additional properties (place, lat, lng) that are not currently on the model. There are two ways you can do this:

1.) Add these fields to the model, and hook into your application so that when a new MerchanDetail is created, run the geocode and store the results along with the model.

2.) Use a RestResource (see the documentation on "Customizing what is returned"). Your REST resource can override the prepare_data() method to run the geocoding, and store the results in the outgoing data.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top