from __future__ import unicode_literals

from rest_framework import serializers

from candidates.serializers import MembershipSerializer

from .models import CandidateResult, ResultSet


class CandidateResultSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = CandidateResult
        fields = (
            'id', 'url',
            'membership',
            'result_set',
            'num_ballots', 'is_winner',
        )

    membership = MembershipSerializer(read_only=True)
    # result_set = ResultSetSerializer(read_only=True)


class ResultSetSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = ResultSet
        fields = (
            'id', 'url',
            'candidate_results',
            'ip_address',
            'num_turnout_reported', 'num_spoilt_ballots',
            # 'post_result',
            'user', 'user_id',
        )
    # post_result = PostResultSerializer()
    user = serializers.ReadOnlyField(source='user.username')
    user_id = serializers.ReadOnlyField(source='user.id')
    candidate_results = CandidateResultSerializer(many=True, read_only=True)

from __future__ import unicode_literals

from rest_framework import serializers

from candidates.serializers import MembershipSerializer

from .models import CandidateResult, ResultSet


class CandidateResultSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = CandidateResult
        fields = (
            'id', 'url',
            'membership',
            'result_set',
            'num_ballots', 'is_winner',
        )

    membership = MembershipSerializer(read_only=True)
    # result_set = ResultSetSerializer(read_only=True)


class ResultSetSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = ResultSet
        fields = (
            'id',
            'url',
            'candidate_results',
            'ip_address',
            'num_turnout_reported',
            'num_spoilt_ballots',
            'user',
            'user_id',
            'ballot_paper_id'
        )
    # post_result = PostResultSerializer()
    user = serializers.ReadOnlyField(source='user.username')
    ballot_paper_id = serializers.ReadOnlyField(
        source='post_election.ballot_paper_id')
    user_id = serializers.ReadOnlyField(source='user.id')
    candidate_results = CandidateResultSerializer(many=True, read_only=True)

Add ballot paper ID to API
Add ballot paper ID to API

  from __future__ import unicode_literals
  
  from rest_framework import serializers
  
  from candidates.serializers import MembershipSerializer
  
  from .models import CandidateResult, ResultSet
  
  
  class CandidateResultSerializer(serializers.HyperlinkedModelSerializer):
      class Meta:
          model = CandidateResult
          fields = (
              'id', 'url',
              'membership',
              'result_set',
              'num_ballots', 'is_winner',
          )
  
      membership = MembershipSerializer(read_only=True)
      # result_set = ResultSetSerializer(read_only=True)
  
  
  class ResultSetSerializer(serializers.HyperlinkedModelSerializer):
      class Meta:
          model = ResultSet
          fields = (
+             'id',
-             'id', 'url',
+             'url',
              'candidate_results',
              'ip_address',
-             'num_turnout_reported', 'num_spoilt_ballots',
+             'num_turnout_reported',
-             # 'post_result',
+             'num_spoilt_ballots',
+             'user',
-             'user', 'user_id',
+             'user_id',
+             'ballot_paper_id'
          )
      # post_result = PostResultSerializer()
      user = serializers.ReadOnlyField(source='user.username')
+     ballot_paper_id = serializers.ReadOnlyField(
+         source='post_election.ballot_paper_id')
      user_id = serializers.ReadOnlyField(source='user.id')
      candidate_results = CandidateResultSerializer(many=True, read_only=True)
  
Add ballot paper ID to API
## Code Before:
from __future__ import unicode_literals

from rest_framework import serializers

from candidates.serializers import MembershipSerializer

from .models import CandidateResult, ResultSet


class CandidateResultSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = CandidateResult
        fields = (
            'id', 'url',
            'membership',
            'result_set',
            'num_ballots', 'is_winner',
        )

    membership = MembershipSerializer(read_only=True)
    # result_set = ResultSetSerializer(read_only=True)


class ResultSetSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = ResultSet
        fields = (
            'id', 'url',
            'candidate_results',
            'ip_address',
            'num_turnout_reported', 'num_spoilt_ballots',
            # 'post_result',
            'user', 'user_id',
        )
    # post_result = PostResultSerializer()
    user = serializers.ReadOnlyField(source='user.username')
    user_id = serializers.ReadOnlyField(source='user.id')
    candidate_results = CandidateResultSerializer(many=True, read_only=True)

## Instruction:
Add ballot paper ID to API
## Code After:
from __future__ import unicode_literals

from rest_framework import serializers

from candidates.serializers import MembershipSerializer

from .models import CandidateResult, ResultSet


class CandidateResultSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = CandidateResult
        fields = (
            'id', 'url',
            'membership',
            'result_set',
            'num_ballots', 'is_winner',
        )

    membership = MembershipSerializer(read_only=True)
    # result_set = ResultSetSerializer(read_only=True)


class ResultSetSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = ResultSet
        fields = (
            'id',
            'url',
            'candidate_results',
            'ip_address',
            'num_turnout_reported',
            'num_spoilt_ballots',
            'user',
            'user_id',
            'ballot_paper_id'
        )
    # post_result = PostResultSerializer()
    user = serializers.ReadOnlyField(source='user.username')
    ballot_paper_id = serializers.ReadOnlyField(
        source='post_election.ballot_paper_id')
    user_id = serializers.ReadOnlyField(source='user.id')
    candidate_results = CandidateResultSerializer(many=True, read_only=True)
