【发布时间】:2019-09-11 21:44:43
【问题描述】:
我有两张表,其中一张名为PlayerLkup,其中包含有关播放器的信息并用作查找表。我有另一个表(称为BattingStats),其中包含玩家的统计数据(和playerid)。 BattingStats 表是一对多的(playerid 被多次列出,每个赛季一次)。
我来自PlayerLkup 的数据显示正常,它使用URL 地址中的playerid 来检索特定播放器。我的问题是如何将我的 BattingStats 模型/表中的数据使用到同一页面上?我假设我的视图页面是需要完成工作的地方。有没有办法将多个模型传递到一个视图中?我试过相同的网址,不同的观点。它似乎对我不起作用。我需要做什么?这里的任何帮助将不胜感激。
我之前发布过这个问题(后来删除了它),但有人错误地将它标记为重复,所以它没有受到任何关注。
models.py
class BattingStats(models.Model):
playerid = models.CharField(db_column='playerID', max_length=9)
year = models.IntegerField(db_column='Year', blank=True, null=True)
g = models.IntegerField(db_column='G', blank=True, null=True)
ab = models.IntegerField(db_column='AB', blank=True, null=True)
r = models.IntegerField(db_column='R', blank=True, null=True)
hr = models.IntegerField(db_column='HR', blank=True, null=True)
rbi = models.IntegerField(db_column='RBI', blank=True, null=True)
sb = models.IntegerField(db_column='SB', blank=True, null=True)
class PlayerLkup(models.Model):
playerid = models.CharField(db_column='playerID', primary_key=True, max_length=255)
birthyear = models.IntegerField(db_column='birthYear', blank=True, null=True)
birthmonth = models.IntegerField(db_column='birthMonth', blank=True, null=True)
birthday = models.IntegerField(db_column='birthDay', blank=True, null=True)
birthstate = models.CharField(db_column='birthState', max_length=255, blank=True, null=True)
birthcity = models.CharField(db_column='birthCity', max_length=255, blank=True, null=True)
namefirst = models.CharField(db_column='nameFirst', max_length=255, blank=True, null=True)
namelast = models.CharField(db_column='nameLast', max_length=255, blank=True, null=True)
weight = models.IntegerField(blank=True, null=True)
height = models.IntegerField(blank=True, null=True)
views.py
def player_info(request, playerid):
playerdata = PlayerLkup.objects.get(playerid=playerid)
return render(request, 'careerstats/playerpage.html', {'playerdata': playerdata})
urls.py
urlpatterns = [
path('player/<str:playerid>/', views.player_info, name='player_info'),
【问题讨论】:
标签: python django django-templates django-views