>
>
> Let me follow up on this. Say I want to add list of all Teams my Players
> played for. What you're saying is that I don't have to add ForeignKey to
> Team and just use team_name field from Team model? Will it work?
>
> This relations stuff is confusing :P.
>
> Haha, no problem! It'll come natural after a while.

Let's start out with the organization of this. There's really two main
relationships you will deal with.

*Foreign Keys*

The first one is ForeignKeys, These are generally used when you want to do
what's called a "One to Many" relationship.

So, for example, let's say you have several teams and several players. We
could assume that each player is *only* playing for one team at any given
time. This means that you will not find a single player playing for two
times. Under this assumption, you would have a "One to Many" relationship
because one team will have many players. *However*, each player will only
have *one* team. That may sound confusing -- read it over a few times if it
does :) Anyways, this scenario would best be solved by using a ForeignKey.

Here's a little visualization just in case that is confusing:

Team: The Bengals
Players: John, Bob, Tim, Joe

Team: The Steelers
Players: Adam, Chris, Frank, Steve

Notice that each player is only playing for one Team. Each team, however,
has multiple players. In this situation, your models would be setup like
this:

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

class Player(models.Model):
    name = models.CharField(max_length=50)
    team = models.ForeignKey(Team)

Now, let's say you want to grab some information about each of these.

1. We have the player Joe, how do we get his team?

print joe.team.name
>> "The Bengals"

2. We have the team "The Steelers", how do we get the list of players?

print steelers.player_set.all()
>> "Adam", "Chris", "Frank", "Steve"

*Many to Many*
*
*
The other common type of relationship you'll run into is called a Many to
Many relationship. This relationship is, essentially, two Models that can
have relationships with many objects from both sides of the relationship.
So to help you understand and visualize how this works, we're going to take
the previous example and modify it a bit.

Let's say we have Two Teams and Several People. However, now people are
allowed to play for Multiple teams. In this example, assume that each
player with the same name is the same person (each name is unique).

Team: The Bengals
Players: Adam, Bob, Charlie

Team: The Steelers
Players: Donald, Evan, Adam

Note: Adam plays for both teams.

So, looking from Adam's perspective, we see that he plays for both the
Steelers and the Bengals. That is -- he has a "many" relationship with the
Teams. Looking at the players from the Team's perspective, it's obvious
that each team has many players. Therefore, in this relationship it is okay
to have Many-to-Many objects from both sides.

Here's the basic Model setup for this:

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

class Player(models.Model):
    name = CharField(max_length=50)
    teams = models.ManyToManyField(Team)

Let's do a couple of examples to see where this is different.

1. Just like before, we have a player but we want to see his Team:

print donald.teams.all()
>> "The Steelers"

2. Now, like before, we want to see this player's team. However, the Player
is no longer restricted to a Single Team.

print adam.teams.all()
>> "The Bengals", "The Steelers"

3. Let's get the reverse relationships -- Who all plays for the Steelers?

print steelers.player_set.all()
>> "Donald", "Evan", "Adam"

So, hopefully, from that example you can see that not much has changed from
the perspective of the Team. It already had a "many" relationship with the
players. However, the thing that has changed here is that the players now
have a "many" relationship with the Teams.

*And to answer your question directly about including the field in the Team:
*
No, you don't need to put a ForeignKey in each Model. You just put it in
once. This will automatically create a variable in your other Model called
<ModelName>_set which can be used to do queries/lookups. Please note that
you can also do something like this to make the naming more practical and
easier to read:

class Team(models.Model)
    name = models.CharField(...)

class Player(models.Model)
    name = models.CharField(...)
    teams = models.ManyToManyField(Team, related_name='players')

Then, you can simply do this:
myteam.players.all() # Notice the word "set" is no longer in there.

Hopefully that helps a bit and I didn't just confuse you more :) Let us
know if you run into any more problems and good luck!

-- 
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