3

I'm trying to create a table on my website and a stackoverflow user suggest me to use django_table2 for it. I try it and in a few minutes I had a nice table, the problem is that I had the table using the classmodel.objects.all(), but I have foreign keys. I try one of their tutorials about to populate table with data, but I was not able to populate with my costume data.

Here is my class:

Here is my model:

class Player_Bios(models.Model):
    my_id            = models.SlugField(unique=True)
    player_id        = models.IntegerField(primary_key=True, max_length=50)    
    name             = models.CharField(max_length=50) 
    last             = models.CharField(max_length=50)

    def __unicode__(self):
       return self.player_id

class BatStat (models.Model):
    player_id           = models.ForeignKey('Player_Bios')
    team_id             = models.ForeignKey('Team')
    bat_stat_id         = models.CharField(max_length=50, unique=True)
    sport_code           = models.CharField(max_length=50, blank=True) 
    ab                  = models.IntegerField(max_length=50, null=True)

def __unicode__(self):
    return self.bat_stat_id

My View:

def SpecificPLayer(request, playerslug):
   table = StatTable(Player_Bios.objects.all())
   RequestConfig(request).configure(table)
   return render(request, 'singleplayer.html', {'table': table})

My Table: # It is suppose to populate the tables, but it did not. Maybe I have to change something in the view, but there was no mention about that in the tutorial

import django_tables2 as tables
player_bios.models import *

data = [
    {"name": "Bradley"},
    {"name": "Stevie"},
     ]  

class StatTable(tables.Table):
   name = tables.Column()

table = StatTable(data)

I was trying to use:

player.batstat_set.all() #but the table does not take it

Is there a way to do it the way the objects..all() works or I can do manually like something like this:

on the Table:

stat = player.batstat_set.all()
for i in stat:

data = [
    {"Player ID": i.player_id},
    {"team ID": i.team_id},
     ]  
amb1s1
  • 1,995
  • 5
  • 22
  • 26

0 Answers0