Skip to content

API Documentation for Serializers

This documentation provides a detailed explanation of the serializers used in the Question-Answer-Vote API for the polls app. The serializers manage the input and output formats for the API endpoints and include validation logic to enforce constraints on the data.

1. VoteInAnswerSerializer

Purpose

Used to serialize the votes associated with a specific answer.

Fields

  • id: Auto-generated unique identifier for the vote.
  • user: The user who cast the vote.
  • answer: The answer being voted on.
  • value: The vote value (+1 for upvote, -1 for downvote).
  • created_at: The timestamp when the vote was created.
  • updated_at: The timestamp when the vote was last updated.

Notes

  • This serializer is used in nested relationships to display votes associated with an answer.

2. AnswerInQuestionSerializer

Purpose

Used to serialize answers for a specific question, including nested vote details.

Fields

  • user: The user who provided the answer.
  • answer: The answer text.
  • vote: A nested list of votes (serialized using VoteInAnswerSerializer).
  • vote_count: The total number of votes for the answer.
  • created_at: The timestamp when the answer was created.
  • updated_at: The timestamp when the answer was last updated.

Notes

  • vote is a read-only field sourced from the reverse relationship answer_vote.

3. QuestionSerializer

Purpose

Used to serialize questions along with their associated answers and vote counts.

Fields

  • id: Auto-generated unique identifier for the question.
  • product: The product the question is related to.
  • user: The user who asked the question. This field uses CurrentUserDefault to auto-assign the authenticated user.
  • question: The question text.
  • answer: A nested list of answers (serialized using AnswerInQuestionSerializer).
  • vote_count: The total number of votes for all answers to this question.
  • created_at: The timestamp when the question was created.
  • updated_at: The timestamp when the question was last updated.

Validation

  • Question Length: The validate_question method ensures that the question text is at least 10 characters long.

Notes

  • answer is a read-only field sourced from the reverse relationship question_answer.
  • created_at and updated_at are read-only fields.

4. AnswerSerializer

Purpose

Used to serialize answers for standalone API endpoints.

Fields

  • id: Auto-generated unique identifier for the answer.
  • question: The question the answer belongs to.
  • user: The user who provided the answer. This field uses CurrentUserDefault to auto-assign the authenticated user.
  • answer: The answer text.
  • vote_count: The total number of votes for the answer.
  • created_at: The timestamp when the answer was created.
  • updated_at: The timestamp when the answer was last updated.

Validation

  • Answer Length: The validate_answer method ensures that the answer text is at least 5 characters long.

Notes

  • created_at, updated_at, and vote_count are read-only fields.

5. VoteSerializer

Purpose

Used to serialize votes for standalone API endpoints.

Fields

  • id: Auto-generated unique identifier for the vote.
  • user: The user who cast the vote. This field uses CurrentUserDefault to auto-assign the authenticated user.
  • answer: The answer being voted on.
  • value: The vote value (+1 for upvote, -1 for downvote).
  • created_at: The timestamp when the vote was created.
  • updated_at: The timestamp when the vote was last updated.

Validation

  • Vote Value: The validate method ensures that the value is either +1 or -1.
  • Unique Vote: The create method ensures that a user cannot vote more than once for the same answer. If a duplicate vote is detected, a ValidationError is raised.

Notes

  • When a vote is created, the corresponding answer's vote_count is updated by adding the value of the vote.

Summary

This serializer structure ensures that: - Data integrity is maintained through field-level and object-level validations. - Relationships between models are efficiently serialized using nested serializers. - Business logic, such as preventing duplicate votes and maintaining minimum text lengths, is enforced in the API layer.