Domain Model - TAGCH/ku-polls GitHub Wiki

Iteration 1 Domain Model

This domain model represents the relationship between the question class and the choice class in iteration 1. The relationship is such that each choice is a response to a question. Multiple choices can be associated with a single question, and each choice can receive votes from respondents.

Iteration 1 Domain Model

Iteration 3 Domain Model

  • Question: Represents a survey or poll question.
    • Relationship: One question can have multiple choices (one-to-many relationship).
  • Choice: Represents a possible answer or option for a question.
    • Relationship: Each choice can receive multiple votes (one-to-many relationship with Vote).
  • Vote: Represents a user's vote for a choice.
    • Relationship: Each vote is linked to one choice and one user (many-to-one relationships).
  • User: Represents an individual who can cast a vote.
    • Relationship: Each vote is made by a single user.

Python Code for Counting Votes:

To count the number of votes for a given choice, use Django’s QuerySet methods efficiently. Here's how you can do it:

from polls.models import Choice, Vote

def votes(choice: Choice) -> int:
    """Return the total number of votes for a choice."""
    return Vote.objects.filter(choice=choice).count()

Note:

Use Django's filter and count methods to count votes. This approach is efficient as it performs the operation in the database. Avoid inefficient methods like instantiating all objects and iterating through them in Python, as shown in the wrong example:

def votes(choice: Choice) -> int:
    count = 0
    for vote in Vote.objects.all():
        if vote.choice == choice:
            count += 1
    return count

The correct method directly leverages the database's ability to filter and count, which is much more efficient.

Iteration 3 Domain Model