Question

i am totally new to both Python and Django,so please excuse me if this question is a bit simpleton.

I am writing a small app to track the scores of a Billiards match. I don't need to explain all the details, but the basic objects involved are:

Team has Players Match is between two Teams (home and away) Match has a collection of Games. Each Game is between two Players (one from each team), excluding players who have already played in the Match.

I have made the following models:

class Team(models.Model):
    team_id = models.IntegerField(unique=True, max_length=5, blank=False,validators=[validate_five_digits])
    name = models.CharField(max_length=50, blank=False, null=False)


class Player(models.Model):
    id = models.IntegerField(unique=True, max_length=5, blank=False,validators=[validate_five_digits])
    team = models.ForeignKey(Team, blank=True, null=True)
    first_name = models.CharField(max_length=50, blank=False, null=False)
    last_name = models.CharField(max_length=50, blank=False, null=False)
    alias_name = models.CharField(max_length=50, blank=True, null=True)
    current_handicap = models.IntegerField()

class Match(models.Model):
    date = models.DateField(blank=False, null=False)
    location = models.CharField(max_length=255, blank=True, null=True)
    table_size = models.CharField(max_length=50, blank=True, null=True)
    home_team = models.ForeignKey(Team, related_name='home_team', blank=True, null=True)
    away_team = models.ForeignKey(Team, related_name='away_team', blank=True, null=True)


class Game(models.Model):
    match = models.ForeignKey(Match, blank=False, null=False)
    match_sequence = models.IntegerField(blank=True, null=True)
    player1 = models.ForeignKey(Player,related_name='player1', blank=False, null=False)
    player2 = models.ForeignKey(Player,related_name='player2', blank=False, null=False)
    player1_handicap = models.IntegerField(null=True, blank=True)
    player2_handicap = models.IntegerField(null=True, blank=True)

I have successfully made Views and ModelForms to add/edit Teams, Players and Matches. The list of Matches is displayed in a table, with 1 match per row....and now I want to put a button to add a new Game.

My plan is to do so by having the button go to a url that looks like this:

game/new/?match_id=1  (or something like that)

Now for the part where i am confused.....When you go to add a new game, i want to display 3 choice fields, and only 3 choice fields. First Choice field should display Players from Home Team that have not yet played a game in this match Second Choice field should display Players from Away Team that have not yet played a game in the match Third Choice Field would only have two choices (Home and Away)....and would indicate which player gets to shoot first.

Then, when user clicks Submit it needs to create a Game() object with the match_id from the query string, the next sequence number for the match, and Player1 = either home or away player, based on 3rd choice field.

I am totally confused about how this should be done....should i be using a forms.Form or a ModelForm?

Any suggestions or skeleton code to clue me in?

Thanks in advance for help with such a newbie question!

Paul

Was it helpful?

Solution

What type of form class to use when building a Django app is a simple problem. If you're editing a model, use a ModelForm, otherwise use a Form.

If you need to limit choices or alter choices based on other data, you can still do that with a ModelForm by overriding the form's __init__()

Using a ModelForm will reduce the amount of code necessary to add the Game instance, because a ModelForm already knows how to create or update an instance of Game. You'd have to handle that yourself if you were just using a Form class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top