I have this API that deletes a recipe using recipe_id that belongs to a user_id. I am trying to create a query before the delete function. I want the app to check if ID exists first, if it's not, flash an error and if it is there, delete the recipe. Thanks.
@app.route('/delete/<int:id>', methods=['DELETE'])
@jwt_required()
def delete_recipe(id):
current_user = get_jwt_identity()
query = Recipe.delete().where((Recipe.poster_id == current_user) & (Recipe.id == id))
return jsonify({'result': query.execute()}), 204
Typically you will raise a 404 not found if the recipe w/the id does not exist.
Peewee can help you with checking existence quite easily: