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)