简体   繁体   中英

Python: How can I append without overriding past append for loop

I am currently trying to append to the output list in my code the id of the query result. I can get it to do one of the ids but it will override the first one how can I change my code to allow any amount of looping to the output.append(q.id)

Here is the code:

@app.route('/new-mealplan', methods=['POST'])

def create_mealplan():

data = request.get_json()
recipes = data['recipes']

output = []

for recipe in recipes:
    try:
        query = Recipes.query.filter(func.lower(Recipes.recipe_name) == func.lower(recipe)).all()
        # print(recipe)
        if query:
            query = Recipes.query.filter(func.lower(Recipes.recipe_name) == func.lower(recipe)).all()
            for q in query:
                output.append(q.id)
    finally:
        return jsonify({"data" : output})

To fix this I removed the Try and Finally blocks. Then returned after the for-loop was completed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM