简体   繁体   中英

django-tables2 customize table visualization

I am working on a web app with Django (first time using this) and I have successfully rendered a table with django-tables2 and it looks like this:

Sequence     Epitope     Score

sequence1    name1       0.5    
sequence1    name2       0.7
sequence2    name1       0.4
sequence2    name2       0.2
...          ...         ...

But I would like to switch the columns and rows to get it to look like:

Sequence     name1     name2     ...
sequence1    0.5       0.7       ...
sequence2    0.4       0.2       ...
...          ...       ...

Is there a way to change this without changing my models? I have been searching for a while now but I can't find a way to change this. Can anyone help me with this?

Here is my table from tables.py

class CSVTables(tables.Table):

class Meta:
    model = CSV_Results
    attrs = {
        'class': 'output_table',
        'th': {'class': 'output_table_header'}
        }
    template_name = 'django_tables2/bootstrap.html'
    fields = ('TCRsequence', 'Epitope', 'Score')#,"Submission_ID")

The model is linked to a form, depending on the input from the user, there could be 10 names in 'Epitope', 50 or just 2,... . My model:

class CSV_Results(models.Model):
TCRsequence = models.CharField(max_length=100)
Epitope = models.CharField(max_length=100)
Score = models.FloatField()
Submission_ID = models.ForeignKey('Info_Submission',on_delete=models.CASCADE)

class Meta:
    verbose_name_plural = "CSV_results"

My views.py:

table = CSVTables(CSV_Results.objects.filter(Submission_ID__Submission_ID__exact=submission_id))
    RequestConfig(request, paginate={'per_page': 50}).configure(table)

And in my html I just rendered the table with:

{% render_table table %}

Thanks!

For fixed set of columns which are not generated dynamically, define table as below and do not associate it with model

class CSVTables(Table):
   sequence = Column(verbose_name='Sequence')
   name1 = Column(verbose_name='Name1')
   name2 = Column(verbose_name='Name2')

Then iterate over queryset objects to generate list of dictionary objects matching to table, which will be passed as argument during table initialization. Each dictionary object should be of format

{ 'sequence' : 'xxxx',
   'name1' : 'yyy',
   'name2' : 'zzz'}

Update:

django-tables2 will not suite for dynamic columns generation. For Dynamic columns client side rendering is a better option. There are couple of JS libraries like datatables might help at client side. A django view app for datatables is django-datatable-view , but I have not tried the dynamic tables in this app. If not, a simple view which can serialize the JSON data as per datatables requirement, which is quite simple, is all what is needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM