Building the course serializer
We are going to create a serializer for the Course
model. Edit the api/serializers.py
file of the courses
application and add the following code highlighted in bold:
# ...
from courses.models import Course, Subject
class CourseSerializer(serializers.ModelSerializer):
class Meta:
model = Course
fields = [
'id',
'subject',
'title',
'slug',
'overview',
'created',
'owner',
'modules'
]
Let’s take a look at how a Course
object is serialized. Open the shell and execute the following command:
python manage.py shell
Run the following code:
>>> from rest_framework.renderers import JSONRenderer
>>> from courses.models import Course
>>> from courses.api.serializers import CourseSerializer
>>> course...