英文:
PUT request with subdocuments
问题
我目前正在为我的后端API创建一个PUT请求,允许用户向数据集中添加餐厅。目前,在数据集中,餐厅的纬度和经度位于geometry > location下。我不确定如何在后端API中指定这一点。
以下是我目前的后端API代码:
@app.route("/api/v1.0/restaurant", methods=["POST"])
def add_new_restaurant():
if "restaurant" in request.form and "city" in request.form:
new_restaurant = {
"restaurant": request.form["restaurant"],
"city": request.form["city"],
"reviews": []
}
new_restaurant_id = restaurants.insert_one(new_restaurant)
new_restaurant_link = "http://localhost:5000/api/v1.0/restaurants/" + str(new_restaurant_id.inserted_id)
return make_response(jsonify({"url": new_restaurant_link}), 201)
希望这对你有所帮助。
英文:
I'm currently creating a PUT request for my backend API to allow users to add restaurants to a dataset. Currently, in the dataset, the lat and lng of the restaurant is under geometry > location. Im unsure how I would specify this in the backend API.
Here is what I have for the backend API so far:
@app.route("/api/v1.0/restaurant", methods = ["POST"])
def add_new_restaurant():
if "restaurant" in request.form and "city" in request.form:
new_restaurant = {
"restaurant": request.form["restaurant"],
"city": request.form["city"],
"reviews": []
}
new_restaurant_id = restaurants.insert_one(new_restaurant)
new_restaurant_link = "http://localhost:5000/api/v1.0/restaurants/" \ + str(new_restaurant_id.inserted_id)
return make_response( jsonify( { "url" : new_restaurant_link } ), 201 )
答案1
得分: 0
这是嵌套对象,因此您可以将它插入为如下所示。您可以用一个映射到一个 dict
值的 geometry
键表示它,该值具有映射到另一个指定经纬度的 dict 的 location
键。
new_restaurant = {
"restaurant": request.form["restaurant"],
"city": request.form["city"],
"reviews": [],
"geometry": {
"location": {
"lat": LAT_VALUE,
"long": LONG_VALUE
}
}
}
英文:
It's a nested object so you can insert it as such. You can represent it in a dict with a geometry
key that maps to a dict
value, which has a location
key that maps to another dict that specifies that lat/long.
new_restaurant = {
"restaurant": request.form["restaurant"],
"city": request.form["city"],
"reviews": [],
"geometry": {
"location": {
"lat": LAT_VALUE,
"long": LONG_VALUE
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论