按日期和小时对其余api数据进行分组。 Django Rest框架 [英] group rest api data by day and hour. Django rest framework

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

问题描述

我是django和其他API框架的超级新手。我有一个正在使用前端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类,这是作业端点的外观,以供参考: jobs-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进行过滤,但是现在我已经在序列化器中完成了操作,这给了我错误作业类型的对象不是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".

这是序列化器的外观:
jobs-by-day-serializer

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

推荐答案

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

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扩展的视图集,而不是modelviewsets,所以我能够返回整数数组。

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 Rest框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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