I know how to reference a single foreign key but when it comes to a foreign key inside another foreign key, I'm lost. I can't think of the correct logic to make this work, which makes me think there's a special logic that applies to this.
What I've tried:
b2=[]
league = League.objects.get(name='International Collegiate Baseball Conference')
team = league.teams.all()
for b in team.curr_players.all():
b2.append(b.first_name + b.last_name)
From my models:
class League(models.Model):
name = models.CharField(max_length=50)
sport = models.CharField(max_length=15)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Team(models.Model):
location = models.CharField(max_length=50)
team_name = models.CharField(max_length=50)
league = models.ForeignKey(League, related_name="teams")
class Player(models.Model):
first_name = models.CharField(max_length=15)
last_name = models.CharField(max_length=15)
curr_team = models.ForeignKey(Team, related_name="curr_players")
all_teams = models.ManyToManyField(Team, related_name="all_players")
found the answer, i had to do a for loop to iterate through what i was receiving from league.teams.all() - which was a team object and then set the iterations equal to a variable and set another iteration equal to variable.related_name.all()
b2=[]
league = League.objects.get(name='International Collegiate Baseball Conference')
team = league.teams.all()
for a in team:
players = a
for b in players.curr_players.all():
b2.append(b.first_name + b.last_name)