Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

252
Visualizações
Python/Django Trying to continually reference foreign keys in multiple classes and be able to print out information from views

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")
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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)
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda