I am using django-panda to make a dynamic table and I have achieved it, the problem I have is that I would like to add more rows to the table using foreign key relationships, in the documentation it says: "to span a relationship, just use the field name of related fields in models, separated by double underscores, "however this does not work with to_pivot_table. I did a test with to_dataframe and here the logic if it gives results, but what I need is to implement it in the pivot
these are my models:
class Student(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True,related_name='student')
quizzes = models.ManyToManyField(Quiz, through='TakenQuiz')
interests = models.ManyToManyField(Subject, related_name='interested_students')
def get_unanswered_questions(self, quiz):
answered_questions = self.quiz_answers \
.filter(answer__question__quiz=quiz) \
.values_list('answer__question__pk', flat=True)
questions = quiz.questions.exclude(pk__in=answered_questions).order_by('text')
return questions
def __str__(self):
return self.user.username
class TakenQuiz(models.Model):
student = models.ForeignKey(Student, on_delete=models.CASCADE, related_name='taken_quizzes')
quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE, related_name='taken_quizzes',verbose_name='Examen')
score = models.FloatField()
date = models.DateTimeField(auto_now_add=True)
objects = DataFrameManager()
and my views
class ResultadosTotales(ListView):
def get(self,request,*args,**kwargs):
examen=TakenQuiz.objects.all()
qs=TakenQuiz.objects.all()
df2=taken.to_dataframe(['student__user__first_name' ,'quiz', 'score'], index='student')
print(df) #here I did the test with the related attribute and if it works
rows = ['student','student__user__first_name'] #Here it does not work even though the documentation uses the same logic
rows = ['student']
cols = ['quiz']
print(df2)
pt = qs.to_pivot_table(values='score', rows=rows, cols=cols,fill_value="falta")
pt=pt.to_html(classes="table table-striped border")
context={ 'pivot': pt
}
return render(request, 'classroom/teachers/resultados_totales.html',context )
espero puedan ayudarme