Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 =