django中chartJS的汇总和组数据

时间:2020-06-30 14:47:17

标签: django charts

我想从我的出勤模型中生成一些图表。我有一个解决方案,但只是想知道是否有更有效的方法。我正在做的只是一个简单的嵌套循环。注释不是很好,因为我认为这会使我丢失的数据消失。

我的模特

```python```
attn_choice = (
    ('1', 'N/A'),
    ('2', 'No Show'),
    ('3', 'Present'),
    ('4', 'Late'),
    ('5', 'Vacation'),
    ('6', 'Sick'),
    ('7', 'Training'),
    ('8', 'Off-Site Work'),
    ('9', 'Approved Delay Start'),
    ('10', 'Not Scheduled'),
    ('11', 'Other'),
)
class Attendance(models.Model):
employee = models.ForeignKey(CustomUser, on_delete=models.DO_NOTHING,
                             limit_choices_to={'is_active': True}, related_name='employee')
supervisor = models.ForeignKey(CustomUser, on_delete=models.SET_NULL, blank=True, null=True,
                               related_name='supervisor', limit_choices_to={'user_type__lte': 2, 'is_active': True})
date = models.DateField()
ac = models.ManyToManyField(Ac, related_name='aircraft', limit_choices_to={'active': True})
remarks = models.TextField(blank=True)
attendance = models.CharField(max_length=2, choices=attn_choice, blank=True)
time_added = models.DateTimeField(auto_now_add=True)
time_modified = models.DateTimeField(auto_now=True)

我的图表的代码

class LineChartJSONView(BaseLineChartView):

cat = [b for a, b in attn_choice]
days = [datetime.today() - timedelta(days=6-a) for a in range(7)]

def get_labels(self):
    return [d.strftime('%d-%b') for d in self.days]

def get_providers(self):
    return self.cat

def get_data(self):
    u = self.request.user
    emp = CustomUser.objects.filter(team__in=u.sup_team.all(), shift__in=u.sup_shift.all())
    data=[]
    for a, b in attn_choice:
        c=[]
        for d in self.days:
            qry = Attendance.objects.filter(date=d,employee__in=emp,attendance=a)
            c.append(qry.count())
        data.append(c)
    return data  

还想知道,不管是否缺少值,什么是最有效的方法。
提前谢谢。
M

0 个答案:

没有答案