Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

255
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda