Skip to content

Commit

Permalink
Added serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
shabda committed Mar 12, 2018
1 parent 4729b87 commit 4b920c0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
14 changes: 8 additions & 6 deletions docs/chapter2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,31 @@ Lets get started with creating serializer class which will serialize and deseria
from rest_framework import serializers
from django.contrib.auth.models import User
from .models import Poll, Choice, Vote
class VoteSerializer(serializers.ModelSerializer):
class Meta:
model = Vote
fields = '__all__'
class ChoiceSerializer(serializers.ModelSerializer):
votes = VoteSerializer(many=True, required=False)
class Meta:
model = Choice
fields = '__all__'
class PollSerializer(serializers.ModelSerializer):
choices = ChoiceSerializer(many=True, read_only=True, required=False)
class Meta:
model = Poll
fields = '__all__'
class VoteSerializer(serializers.ModelSerializer):
class Meta:
model = Vote
In the above lines of code we created a Choice Serializer in such a way that whenever we create a choice it does need to have the votes model connected to it and if a poll is created the choices needs to be created simultaneously.

Expand Down
28 changes: 28 additions & 0 deletions pollsapi/polls/serialzers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from rest_framework import serializers

from .models import Poll, Choice, Vote


class VoteSerializer(serializers.ModelSerializer):
class Meta:
model = Vote
fields = '__all__'


class ChoiceSerializer(serializers.ModelSerializer):
votes = VoteSerializer(many=True, required=False)

class Meta:
model = Choice
fields = '__all__'


class PollSerializer(serializers.ModelSerializer):
choices = ChoiceSerializer(many=True, read_only=True, required=False)

class Meta:
model = Poll
fields = '__all__'



0 comments on commit 4b920c0

Please sign in to comment.