django-pivot icon indicating copy to clipboard operation
django-pivot copied to clipboard

Bug with binary fields

Open P3RI9 opened this issue 3 years ago • 0 comments

Hi! I have detected what I think is a bug with this library when using binary fields on a django model. The problem is that answers aren't shown right, just as 0.0 values Code to reproduce this bug: Models:

from django.db import models
class Student(models.Model):
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name


class Subject(models.Model):
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name


class Grades(models.Model):
    student = models.ForeignKey(Student, on_delete=models.CASCADE)
    subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
    marks = models.BinaryField()

    def __str__(self):
        return f"{self.student}, {self.subject}, {self.marks}"

Code to fill models with django-shell

from core.models import *
from django_pivot.pivot import pivot

students = ['Jacob', 'Ameliee']
subjects = ['Math', 'Science']
marks = [[b'Bilbao', b'Mariano'], [b'Murcia', b'Concha']]

for i, student_name in enumerate(students):
	st = Student(name=student_name)
	st.save()
	for j, subject_name in enumerate(subjects):
		su = Subject(name=subject_name)
		su.save()
		g = Grades(student=st, subject=su, marks=marks[i][j])
		g.save()

pivot(Grades, 'student__name', 'subject__name', 'marks')

When you change Grades.marks to a SmallIntegerField (in example) it works fine. Could you help me?

P3RI9 avatar Feb 10 '22 13:02 P3RI9