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!

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 trial

Python Django REST Framework RESTful Django Create Serializers

41 Points

GameSerializer's model isn't Game; PlayerSerializer's model isn't Player; ScoreSerializer's model isn't Score. Why??

The question asks to create serializers for the 3 models, namely Game, Player and Score and name them as (ModelName)Serializer.

from rest_framework import serializers
from . import models
#create a ModelSerializer for each of the three models, Game, Player, and Score. Name them ModelNameSerializer, e.g. PlayerSerializer.
#This Serializer is meant for a feedback page. classes are defined accordingly.
#serializers are classes,hence, we will define classes
#Convert Model Instances into JSON and backend and reverse.
class GameSerializer(serializers.ModelSerializer):
    class Meta:
        models = models.Game
        #in models.py the fields are defined
        #fields = ()

#We need to explicitly specify which fields are needed to be serialized, so we define them under fields
class PlayerSerializer(serializers.ModelSerializer):
    class Meta:
        models = models.Player
        #fields = ()

class ScoreSerializer(serializers.ModelSerializer):
    class Meta:
        models = models.Score
        #fields = ()

1 Answer

instead of models = try model =