Question

I want to build a table that shows statistics for baseball games. I currently have a model set up to represent one baseball game.

class Baseball(models.Model):
    gametime = models.DateTimeField()
    vteam = models.CharField(max_length=255)
    vpitcher = models.CharField(max_length=255)
    hteam = models.CharField(max_length=255)
    hpitcher = models.CharField(max_length=255)

There are other fields as well, but this is sufficient to explain what I'm trying to do.

I'm using django-tables2 to render my tables. I defined a Table class

class BaseballTable(tables.Table):
    class Meta:
        model = models.Baseball

And my view

class Home(SingleTableView):
    model = models.Baseball
    table_class = tables.BaseballTable

But the default behavior is not what I want. The tables are showing one row per entry in my database.

gametime     vteam     vpitcher     hteam     hpitcher
date1        vteam1    vpitcher1    hteam1    hpitcher1
date2        vteam2    vpitcher2    hteam2    hpitcher2

Instead I want the rows of the table to not mimic the database model directly, but alternate rows as follows so the 1st and 3rd row pull from the same place but the 2nd and 4th row are different.

gametime     team     pitcher
date1        vteam1   vpitcher1
<blank>      hteam1   hpitcher1
date2        vteam2   vpitcher2
<blank>      hteam2   hpitcher2

Does anyone know if django-tables2 can do this or if I need to write my own tables?

Was it helpful?

Solution

I believe that you have a reasonable question.

First of all, I reocmmend using a normal Table and not a ModelTable since you will need to override all of you columns after all.

Different kinds of data in rows is NOT something that django-tables2 supports out of the box so documentation doesn't say anything about that. However, there are many methods you can use to achieve what you want.

One way is to arrange the data you pass to the table in such way that one row will contain the date1 - vteam1 - vpitcher1 and the next row will contain blank - hteam1 - hpitcher1. To do this you will create a new list with your table data and put in it records from your original query data. You must be very carefull with the sorting if you allow this solution.

Another easier way is to create your queries so that they will return date1, vteam<br/>hteam1, vpitcher1<br/>hpicher1. So when you render this you will get vteam1 and hteam1 in different rows ! Because I can't test it right now, if this does not seem to work then probably <br> is not safe so you should output the value enclosed in mark_safe.

To do that just override your render_row for team and pitcher and return mark_safe(value) (or create a new class TwoRowsColumn that will print your value however you like - you may even create a small html table that will contain two rows with hteam and vteam and will be printed in the cell for hteam/vteam). Here the sorting is easier however you won't be able to sort with the hteam or hpitcher.

There are other ways - for instance subclassing ModelTable to do what you want however I don't recommend it since your requirement is very specific and wouldn't be easily reused anywhere else.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top