MySQL Lite Django Multitables -- mysql campo con django campo con python-2.7 camp Relacionados El problema

MySQL lite django multitables


0
vote

problema

Español

Estoy trabajando en un proyecto de la escuela y una de las preguntas es una lista de las ligas con un jugador (actual) llamado "Sophia". He podido conseguir todo el período de las ligas, y todos los jugadores nombran a Sophia. Sin embargo, no puedo entender cómo comenzar cómo comenzar en la última tabla donde el primer nombre es referencia a la primera tabla donde las ligas deben obtener la respuesta que necesito. Espero que alguien pueda ver lo que estoy teniendo problemas al ver. He vinculado los modelos y puntos de vista a continuación.

  Manage.py  from django.db import 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")   views.py   from django.shortcuts import render, redirect from .models import League, Team, Player  from . import team_maker  def index(request):     context = {         "leagues": League.objects.all(),         "teams": Team.objects.all(),         "players": Player.objects.all(),         "baseball" : League.objects.filter(sport__contains="Baseball"),         "women" : League.objects.filter(name__contains="Womens'"),         "ice" : League.objects.filter(sport__contains="ice"),         "nofootball" : League.objects.exclude(sport__contains="football"),         "conferences" : League.objects.filter(name__contains="conference"),         "atlantic": League.objects.filter(name__contains="Atlantic"),         "teamdallas": Team.objects.filter(location__contains="Dallas"),         "raptor": Team.objects.filter(team_name__contains="Raptor"),         "cityloc": Team.objects.filter(location__contains="city"),         "startT": Team.objects.filter(team_name__startswith="T"),         "abc": Team.objects.order_by('location'),         "cba": Team.objects.order_by('location').reverse(),         "cooper": Player.objects.filter(last_name="Cooper"),         "joshua": Player.objects.filter(first_name="Joshua"),         "nocooper": Player.objects.filter(last_name="Cooper").exclude(first_name="Joshua"),         "wyatt": Player.objects.filter(first_name="Alexander")|Player.objects.filter(first_name="Wyatt"),         "atlanticsoccer" : Team.objects.filter(league__name__contains='atlantic', league__sport__contains="soccer"),         "bostonp" : Player.objects.filter(curr_team__team_name__contains='Penguins'),         "icbc" : Player.objects.filter(curr_team__league__name__contains='International Collegiate Baseball '),         "footballlopez" : Player.objects.filter(curr_team__league__name__contains="American Conference of Amateur Football", last_name__contains="lopez"),         "footballplayer" : Player.objects.filter(curr_team__league__sport__contains="football"),         "sophia" : Team.objects.filter(curr_players__first_name__contains='sophia'),         "sophiale" : Team.objects.filter(league__contains='sophia'),      }          print Team.objects.filter(league__contains='sophia')         return render(request, "leagues/index.html", context)  def make_data(request):     team_maker.gen_leagues(10)     team_maker.gen_teams(50)     team_maker.gen_players(200)      return redirect("index")  index.html  just the part you need  <h5>Question 8</h5>         {% for player in sophiale %}         <li>{{player.name}}</li>         {% endfor %}         </ol>   
Original en ingles

I am working on a project from school and one of the questions is List all leagues with a (current) player named "Sophia". I have been able to get all the leagues period, and all the players name Sophia. How ever I cannot seem to figure out how to start in the last table where the first name is reference the first table where the leagues are to get the answer I need. I am hoping someone can see what I am having issues seeing. I have linked the models and views below.

Manage.py  from django.db import 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")   views.py   from django.shortcuts import render, redirect from .models import League, Team, Player  from . import team_maker  def index(request):     context = {         "leagues": League.objects.all(),         "teams": Team.objects.all(),         "players": Player.objects.all(),         "baseball" : League.objects.filter(sport__contains="Baseball"),         "women" : League.objects.filter(name__contains="Womens'"),         "ice" : League.objects.filter(sport__contains="ice"),         "nofootball" : League.objects.exclude(sport__contains="football"),         "conferences" : League.objects.filter(name__contains="conference"),         "atlantic": League.objects.filter(name__contains="Atlantic"),         "teamdallas": Team.objects.filter(location__contains="Dallas"),         "raptor": Team.objects.filter(team_name__contains="Raptor"),         "cityloc": Team.objects.filter(location__contains="city"),         "startT": Team.objects.filter(team_name__startswith="T"),         "abc": Team.objects.order_by('location'),         "cba": Team.objects.order_by('location').reverse(),         "cooper": Player.objects.filter(last_name="Cooper"),         "joshua": Player.objects.filter(first_name="Joshua"),         "nocooper": Player.objects.filter(last_name="Cooper").exclude(first_name="Joshua"),         "wyatt": Player.objects.filter(first_name="Alexander")|Player.objects.filter(first_name="Wyatt"),         "atlanticsoccer" : Team.objects.filter(league__name__contains='atlantic', league__sport__contains="soccer"),         "bostonp" : Player.objects.filter(curr_team__team_name__contains='Penguins'),         "icbc" : Player.objects.filter(curr_team__league__name__contains='International Collegiate Baseball '),         "footballlopez" : Player.objects.filter(curr_team__league__name__contains="American Conference of Amateur Football", last_name__contains="lopez"),         "footballplayer" : Player.objects.filter(curr_team__league__sport__contains="football"),         "sophia" : Team.objects.filter(curr_players__first_name__contains='sophia'),         "sophiale" : Team.objects.filter(league__contains='sophia'),      }          print Team.objects.filter(league__contains='sophia')         return render(request, "leagues/index.html", context)  def make_data(request):     team_maker.gen_leagues(10)     team_maker.gen_teams(50)     team_maker.gen_players(200)      return redirect("index")  index.html  just the part you need  <h5>Question 8</h5>         {% for player in sophiale %}         <li>{{player.name}}</li>         {% endfor %}         </ol> 
        

Lista de respuestas

2
 
vote
vote
La mejor respuesta
 

Intenta esto:

  League.objects.filter(teams__ all_players__first_name__contains='sophia')   
 

Try this:

League.objects.filter(teams__ all_players__first_name__contains='sophia') 
 
 
   
   

Relacionados problema

2  Herencia de Python Best Practice para 'Historias de objetos'  ( Python inheritance best practice for object histories ) 
Tengo una serie de clases (por ejemplo, contacto) para las cuales también quiero rastrear el historial de cambios. A continuación se muestra mi punto de parti...

1  ¿Codificando una lista de tuplas con Python?  ( Encoding a list of tuples with python ) 
Estoy leyendo de un directorio de texto UTF-8, luego inserto el texto lectorizado en una lista y estoy obteniendo algunas tuplas como esta: l = [('mucho','...

0  Ejecutar script shell (.csh) en Cygwin con Python  ( Run shell script csh in cygwin using python ) 
He creado una herramienta de Python que genera scripts y amplificadores de shell; ejecútelo usando el comando subprocesos.popen ("csh -l -c" + os.environ ["...

1  No se puede localizar la etiqueta HTML para raspar  ( Unable to locate html tag for scraping ) 
No soy genial en HTML, así que estoy un poco tropezado por esto. Estoy tratando de raspar las publicaciones de DateTime de Instagram utilizando Python y se ...

0  Pygame, estoy tratando de hacer un temporizador  ( Pygame i am trying to make a timer ) 
He creado un juego que tiene dos autos que se rarcan con los botones tocando más rápido que el otro. Así que quiero hacerlo donde después de la carrera termin...

1  Cómo matar el proceso por nombre en Python  ( How to kill process by name in python ) 
>>> os.system('adb shell pidof logcat') 750 4774 0 >>> os.system('adb shell ps | grep logcat') root 750 1 8760 1720 __skb_recv 7f8f5a5edc S ...

-3  Modelo inesperado - Python Def  ( Unexpected indent python def ) 
¿Puedes ayudarme con esto? def group_iter (iterator, n=2, strict=False): accumulator = [] accumulator.append(item) if len(accumulator)...

0  Añadiendo números a una lista  ( Appending numbers to a list ) 
urllist = ['http://example.com', 'http://example1.com'] i = 0 while i < len(urllist): source = urllib.urlopen(urllist[i]).read() regex = '...

0  Error en la cadena de codificación  ( Error in encoding string ) 
Este podría ser un problema muy simple, pero simplemente no puedo resolverlo. en mi consola de python i hago: request.request.data.get('comment') i o...

0  Archivo / Zipfile Python  ( Archive zipfile python ) 
El ejemplo delfildeo zip en el texto de la lección almacena la ruta completa de los archivos que se guarda con el shipfile. Normalmente, sin embargo, Zipfiles...




© 2022 respuesta.top Reservados todos los derechos. Centro de preguntas y respuestas reservados todos los derechos