我编写了一个代码,通过将id,category_id字段作为可选参数传递并获得正确的结果来查找文档。如何使用request.args.get方法使参数可选?
这是我的代码:
@reviews.route('/<inp_id>/', defaults={'cat_id': None}, methods=['GET'])
@reviews.route('<inp_id>/<int:cat_id>', methods=['GET'])
def index(inp_id, cat_id):
db_collection = mongo_connection.db.table_name
if cat_id != None:
res = collection.find_one({'id': inp_id, 'category.id': cat_id})
result = TatvamObjDict.load_fromObject(res)
else:
res= collection.find_one({'id': inp_id})
result = TatvamObjDict.load_fromObject(res)
return make_response(jsonify(result), 200)