按天和小时分组休息api数据.Django 休息框架 [英] group rest api data by day and hour. Django rest framework

查看:39
本文介绍了按天和小时分组休息api数据.Django 休息框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 django 和其他 API 框架非常陌生.我有一个项目,我正在使用 vueJS 和 vueJS 作为前端.我需要为图表序列化一些数据.

I'm super new to django and the rest API framework. I have a project that I am working on using both and vueJS for the front end. I need to serialize some data for a chart.

对于 API 端点之一,我试图像这样对数据进行分组:

For one of the API end points I am trying to group the data like so:

    "day_of_the_week": {
        "9am":[{"job":".."}],
        "10am":[{"job":"..."}],
        "11am": [{"job": ".."}],
        ...
    }

我正在使用 Job 类,作为参考,这是作业端点的样子:作业 API

I am using a Job class, for reference this is how the jobs end point looks like: jobs-api

因此,我创建了一个新的端点,而不是我在图片上看到的,我将只显示一个包含任何给定日期数据的对象.在前端有一个带有过滤器的图表,让用户可以按他们请求的日期过滤作业.加载时,当用户没有给出星期几时,终点将返回今天"的对象.

So instead of what i have on the picture I am creating a new endpoint where i will only show one object that contains the data for any given day. On the front end there is a chart with filters that let the user filter the jobs by the day they request. On load, when the user has given no day of the week, the end point will return the object of 'today'.

因为我是新手,我不知道在哪里做这件事,我最初的想法是过滤views.py,但现在我已经在序列化程序中完成了,这给了我错误Object of类型 Job 不是 JSON 可序列化的".

Because I am new to this, I have no idea where to do this, my initial thought was to filter on the views.py, but for now I have done it in the serializer which gives me the error "Object of type Job is not JSON serializable".

这是序列化程序的样子:每日作业序列化器

This is how the serializer looks like: jobs-by-day-serializer

很明显,有些东西我不太了解,所以任何帮助将不胜感激.

Clearly, there is something that I am not quite grasping, so any help would be appreciated.

这是我现在的 views.py,我已经为查询集添加了过滤器以按天过滤,所以我现在可以按天过滤:jobs_by_day_viewset

this is my views.py now, I have added the filter for the queryset to filter by day, so I can filter by day now: jobs_by_day_viewset

推荐答案

给出的答案为问题的一部分提供了解决方案.我设法解决了第二部分.一旦我可以按星期几过滤我的查询集,我仍然必须按开始的小时对每个对象进行分组.我是这样解决的:

The answers given provide a solution for one part of the question. I managed to solve the second part. Once i could filter my queryset by day of the week I still had to group every object by the hour in which it had been started. This is how i solved it:

 def get(self, request):
    params = self.get_params()
    queryset = Job.objects.filter(
        dt_start__week_day=params['day'], dt_start__gte=params['dt_start'], dt_end__lte=params['dt_end'])
    queryset_started = queryset.annotate(hour=ExtractHour('dt_start'))
    queryset_ended = queryset.annotate(hour=ExtractHour('dt_end'))
    started = []
    ended = []
    total_jobs =[]
    for n in range(0, 23):
        queryset_end = queryset_ended.filter(hour=n)
        ended.append(round(queryset_end.count() / queryset.count() * 100, 2))
        queryset3 = queryset_started.filter(hour=n)
        started.append(round(queryset3.count() / queryset.count() * 100, 2))
    for x in range(len(started)):
        total_jobs.append(round(started[x]+ended[x], 2))

我使用了一个从 APIview 扩展而来的视图集,而不是模型视图集,以便我能够返回一个整数数组.

I used a viewset that extended from APIview instead of the modelviewsets so that i was able to return an array of integers.

这篇关于按天和小时分组休息api数据.Django 休息框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆