django-tables change header with variable

22 views
Skip to first unread message

Sixtine Vernhes

unread,
May 11, 2017, 8:10:59 AM5/11/17
to Django users

Using django-tables2 I try to have 2 dimensions table. I tried to make this using accessors with the docs here But it doesn't work at all.

I don't seen any example and I don't know if it's possible. Has anyone has an idea?

Here my code :



#views.py
def visualiserTableResult(request):
   choix
= request.POST['type_donnees']
   sample
= Sample.objects.all()
   antibiotique
= Antibiotique.objects.all()
   data
= []
   
for s in sample:
     
for a in antibiotique:
        echantillon
= Echantillon.objects.filter(antibiotique_id=a.antibio_id).filter(sample_id=s.sample_id).values(
           
'echantillon_id')
       
if choix == "C":
            res
= Resultats.objects.filter(echantillon_id=echantillon).values_list("res_C")
       
if choix == "S":
            res
= Resultats.objects.filter(echantillon_id=echantillon).values_list('res_S', flat=True)
       
if choix == "d":
            res
= Resultats.objects.filter(echantillon_id=echantillon).values_list('res_d', flat=True)

        data
.append({s: {a:  res}})
   table
= GeneralTable(data)
   
RequestConfig(request).configure(table)
   
return render(request, 'resistance/visualiserTableResult.html', {'table': table})

#tables.py
class GeneralTable(tables.Table):
   sample
= tables.Column(accessor=('s'))
   antibiotique
= tables.Column(attrs={'th': {'antibiotique': 'a'}})

   
class Meta:
     attrs
= {'class': 'paleblue'}

#models.py
class Echantillon (models.Model):
   echantillon_id
= models.IntegerField(primary_key=True)
   antibiotique
= models.ForeignKey('Antibiotique', on_delete=models.CASCADE)
   sample
= models.ForeignKey(Sample, on_delete=models.CASCADE)
   resistance
= models.ManyToManyField(
   
'Type_resistance',
    through
='Resultats'
)

class Resultats (models.Model):
   resultats_id
= models.IntegerField(primary_key=True)
   echantillon
= models.ForeignKey('Echantillon', on_delete=models.CASCADE)
   resistance
= models.ForeignKey('Type_resistance', on_delete=models.CASCADE)
   res_C
= models.DecimalField(max_digits=5, decimal_places=2,null=True)
   res_S
= models.CharField(max_length=10, null=True)
   res_d
= models.DecimalField(max_digits=5, decimal_places=2, null=True)
   comment
= models.TextField(null=True)

I want to have a table with values of "antibiotique" in headers and result for each sample.I hope that someone have an idea

Reply all
Reply to author
Forward
0 new messages