I am writing an API using flask.
I would like the API to return either the elements in the column category or in the column subcategory of my table category depending on the url.
eg http:.../categories/subcategory or http:.../categories/category
I have tried to create the variable category_type and to assign it to the output d but it does not work.
class Categories(db.Model):
__tablename__ = 'categories'
category = db.Column(db.Text)
subcategory = db.Column(db.Text, primary_key = True)
@app.route('/categories/<string:category_type>', methods=['GET'])
def categories(category_type):
if request.method == 'GET':
results = Categories.query.all()
json_results = []
for result in results:
d = {'Category': result.category_type}
json_results.append(d)
return jsonify(items=json_results)
result.category_typeisn't defined in the Categories class. Perhaps you want a filter?