Instead of including every player in the Game model, I would add a
field to the Player model to assign which team a player belongs to:

class Team(models.Model):
    name = models.CharField(max_length=60)

class Player(models.Model):
    surname = models.CharField(max_length=60)
    lastname = models.CharField(max_length=60)
    team = models.ForeignKey(Team)

Now you can just specify the home_team and the away_team in the Game
model without having to add every player.

For more functionality (like allowing players to switch teams) you
could use a through table that includes Player, Team, join_date and
leave_date -- The example in the docs is a good starting point for
that: http://docs.djangoproject.com/en/dev/topics/db/models/

I'm not sure how to answer a), but you can figure out b) because the
goal_scorer will be assigned to either home_players or away_players in
your original Game model, or the goal_scorer will be assigned to the
home_team or away_team in the modified models.

On Oct 5, 7:21 am, "c!w" <wittwe...@gmail.com> wrote:
> Hi,
> I trying to create a hockey database, based on Django.
> The heaviest part so long is to define the structure of my models.
> There I need some help.
> What I have so far..
>
> class Team(models.Model):
>     name = models.CharField(max_length=60)
>
> class Player(models.Model):
>     surname = models.CharField(max_length=60)
>     lastname = models.CharField(max_length=60)
>
> class Game(models.Model):
>     date_time = models.DateTimeField()
>     home_team = models.ForeignKey(Team,related_name='home_team')
>     away_team = models.ForeignKey(Team,related_name='away_team')
>     home_players = models.ManyToManyField(Player,blank=True)
>     away_players = models.ManyToManyField(Player,blank=True)
>
> class GameGoal(models.Model):
>     goal_scorer = models.ForeignKey(Player,blank=True,null=True)
>     first_assist = models.ForeignKey(Player,blank=True,null=True)
>     game = models.ForeignKey(Game)
>
> So now I have the following problems with these models.
>
> a) How can I limit the choice (in the admin page) of goal_scorer to
> Players, which are assigned to the Game?
> b) A GameGoal should be connected to the home_team or the away_team,
> how can I handle this? Add a foreignkey to Team and  limit the choice
> to the both teams?
> c) Is there a better way, to define such a case?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to