django noob question

14 views
Skip to first unread message

yousuf

unread,
Jan 26, 2012, 2:37:56 PM1/26/12
to Django users
this is my models.py

from django.db import models

# Create your models here.

class Leagues(models.Model):
LeagueName = models.CharField(max_length=200)

class Team(models.Model):
TeamName = models.CharField(max_length=200)

class LeagueTable(models.Model):
league = models.ForeignKey(Leagues)
team = models.CharField(max_length=200)
matches_played = models.IntegerField()
matches_won = models.IntegerField()
matches_drawn = models.IntegerField()
matches_lost = models.IntegerField()
points = models.IntegerField()

class FixtureTable(models.Model):
league = models.ForeignKey(Leagues)
fixture_date = models.DateField('Fixture Date')
team_one = models.CharField(max_length=200)
team_one_score = models.IntegerField()
team_two = models.CharField(max_length=200)
team_two_score = models.IntegerField()
in the "class FixtureTable", i want team_one and team_two to be linked
to two differnt teams in "class Team" models. how to create multiple
relations, or is it possible.

PS: this is purely a noob, with a little experience in programming,
but no experience with either python or databases.

PPS: I found that the best way to learn is to create your own problem,
and solve the problem in parallel while following the tutorial. so i
am just stuck in the beginning. thankyou.

thankyou.

adj7388

unread,
Jan 27, 2012, 4:03:40 PM1/27/12
to Django users
Without knowing your problem domain, you might want to start off with:

class League(models.Model):
name = models.CharField(max_length=200)

class Team(models.Model):
name = models.CharField(max_length=200)
league = models.ForeignKey(League)

Generally you don't want a model with a plural name (like 'Leagues').
When you're dealing with more than one league, you will get leagues
out of your League model with:

my_leagues = League.objects.all()

which will return a queryset with all your leagues.

Likewise, for teams:

my_teams = Team.objects.all()

If you want to know the league for a specific team:

my_team = Team.objects.get(pk=1)
my_team_league = my_team.league

HTH,
Alan

adj7388

unread,
Jan 27, 2012, 6:04:01 PM1/27/12
to Django users
I forgot to address the issue of your LeagueTable, which appears to be
a way to keep track of standings. It looks like what you're really
doing is trying to keep track of matches won and lost, points earned,
etc i.e., the standings of a league.

One way to do this is with a Match model:

class Match(models.Model):
date = models.DateField()
home_team = models.ForeignKey(Team)
visiting_team = models.ForeignKey(Team)
home_score = models.IntegerField()
visiting_score = models.IntegerField()

For each match you would create an object in your Match table, with
the score for each team. Having this data would allow you to calculate
1) which team won, 2) number of points earned, etc.

So just querying the Match table would allow you to calculate on-the-
fly the record for each team, the number of points, then sort by the
number of points to produce a Standings queryset. I hope that makes
sense without giving examples of all the queries.

This is just one way to do what I think you're trying to do. It may
not be the best way. But hopefully it gives you some ideas about how
to use Django models.

Alan
Reply all
Reply to author
Forward
0 new messages