Nick T
Nick T

Reputation: 683

How to display data from two different models on the same HTML page

I have two tables one called called PlayerLkup, which contains info about the player and serves as a lookup table. I have another table (called BattingStats) which contains a player's stats (and the playerid). The BattingStats table is a one-to-many (playerid is listed multiple times, one time for each season they played).

My data from PlayerLkup is displaying fine, and it is using the playerid in the URL address to retreive a specific player. My question is how do I use the data from my BattingStats model/table onto that same page? I'm assuming my views page is where the work needs to be done. Is there a way to have multiple models passed into one view? I've tried same url, different views. It didnt seem to work for me. What do I need to do? Any help here would be appreciated.

I posted this question before (and have since deleted it) but someone mistakenly tagged it as a duplicate so it didn't receive any attention.

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'),

Upvotes: 1

Views: 885

Answers (2)

Raydel Miranda
Raydel Miranda

Reputation: 14360

Just in case you don't want to change your model (I would) you can:

def player_info(request, playerid):
    playerdata = PlayerLkup.objects.get(playerid=playerid)
    battingdata = BattingStats.objects.get(playerid=playerid)
    data = {
         'playerdata': playerdata,
         'battingdata': battingdata
    }
    return render(request, 'careerstats/playerpage.html', data)

In your template, you will both playerdata and battingdata available as template variables.

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599510

You can do whatever you like in the view, and pass as many models as you like in the context.

But in your case you don't actually need to change the view, but you should change the models. It looks like BattingStats should have a relationship to PlayerLkup, via the playerid field. You should make this a ForeignKey (and call it player) - note, you don't actually need to change the underlying tables at all, which may be important since this appears to be a legacy db.

class BattingStats(models.Model):
    player = models.ForeignKey('PlayerLkup', db_column='playerID')

Now in your template you can do:

{{ playerdata.namefirst }}
{% for stat in playerdata.battingstats_set.all %}
  {{ stat.year }}
  {{ stat.g }}
  ... 
{% endfor %}

(Also, please give better names to your fields. They don't need to be the same as the db column, that's what the db_column attribute is for. There's no reason to use single-character field names like g.)

Upvotes: 2

Related Questions