Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKennedy Malonga
22,856 PointsIn postman it returns an empty list or array when using marshal to get the courses in the course_field
status code 200/ok but returns empty array for courses in course_field using marshal
1 Answer
Kennedy Malonga
22,856 Pointsfrom flask import jsonify, Blueprint
from flask_restful import (Resource, Api, reqparse, inputs, fields, marshal, marshal_with)
import models
course_fields = { 'id': fields.Integer, 'title': fields.String, 'url': fields.String, 'reviews': fields.List(fields.String) }
class CourseList(Resource): def init(self): self.reqparse = reqparse.RequestParser() self.reqparse.add_argument( 'title', required=True, help='No course title provided', location=['json', 'form'] ) self.reqparse.add_argument( 'url', required=True, help='No course URL provided', location=['json', 'form'], type=inputs.url ) super().init()
def get(self):
courses = [marshal(course, course_fields)
for course in models.Course.select()]
return {'courses': courses}
def post(self):
args = self.reqparse.parse_args()
models.Course.create(**args)
return jsonify({'courses': [{'title': 'Python Basics'}]})
class Course(Resource): def get(self, id): return jsonify({'title': 'Python Basics'})
def put(self, id):
return jsonify({'title': 'Python Basics'})
def delete(self, id):
return jsonify({'title': 'Python Basics'})
courses_api = Blueprint('courses', name) api = Api(courses_api) api.add_resource( CourseList, '/api/v1/courses', endpoint='courses' ) api.add_resource( Course, '/api/v1/courses/<int:id>', endpoint='course' )
this is the code i need the answer please