【问题标题】:Joining two tables django newbie qustion加入两个表 django 新手问题
【发布时间】:2017-03-19 16:08:46
【问题描述】:

mysqllite 表加入 django 对我来说是新的。我可以在一张桌子上查询就好了。并且有点通过原始 mysql 加入。我需要的是一个如何将表格一起编码的示例,即在模型下方,我的项目的第一个问题是找到大西洋会议中的所有团队

我有 考虑到这一点

  "atlanticsoccer": League.objects.filter(name__contains="atlantic")

 this in html
    <h5>Question 1</h5>
                {% for whatever in atlanticsoccer %}
                <li>{{whatever.name}}</li>
                <li></li>
                {% endfor %}
                </ol>

which gives me all my Atlantic conference leauges. But i can't seem to find an example of how to join the teams by id . I have linked below the models view If i can get an example of how to do the first I can figure out the html. Thanks in advance for helping a new coder.

Models.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(),

        "atlanticsoccer": League.objects.filter(name__contains="atlantic")


    }
    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

    <h5>Question 1</h5>
        {% for whatever in atlanticsoccer %}
        <li>{{whatever.name}}</li>
        <li></li>
        {% endfor %}
        </ol>

【问题讨论】:

    标签: django python-2.7 django-models


    【解决方案1】:

    首先获取所有league 名称为“atlantic”的teams

    atlanticsoccer = Team.objects.filter(league__name__contains='atlantic')
    

    然后在您的模板中遍历它们并打印您想要的任何内容:

    <ul>
        {% for team in atlanticsoccer %}
            <li>{{team.location}}</li>
            <li>{{team.team_name}}</li>
            <li>{{team.league.name}}</li>
            <li>{{team.league.sport}}</li>
        {% endfor %}
    </ul>
    

    【讨论】:

    • Ty 那是我正在寻找的示例,现在我可以做剩下的了。
    • 我的朋友好样的。编码愉快!
    • 如果你还在那里,是的,这会提取我需要的所有数据,是否有技巧将球队从特定运动和联赛中拉出来?
    • 当然。试试这个:Team.objects.filter(league__name__contains='atlantic', league__sport__contains="some_sport")
    • 再次感谢我会弄清楚 html。感谢帮助
    猜你喜欢
    • 2010-10-11
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    • 2017-12-09
    相关资源
    最近更新 更多