Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django ORM-列表中计算的时间差之和_Django_Datetime_Orm_Aggregation - Fatal编程技术网

Django ORM-列表中计算的时间差之和

Django ORM-列表中计算的时间差之和,django,datetime,orm,aggregation,Django,Datetime,Orm,Aggregation,巨蟒新手在这里。。。在我的Django模型中,我有一系列带有开始和停止时间的客房预订活动,我需要检索每个活动在一个月内的总持续时间,按公司分组。当前看起来如下所示: models.py: class RoomBooking(models.Model): room = models.ForeignKey(Room) bookedBy = models.ForeignKey(User) startTime = models.DateTimeField() stopTi

巨蟒新手在这里。。。在我的Django模型中,我有一系列带有开始和停止时间的客房预订活动,我需要检索每个活动在一个月内的总持续时间,按公司分组。当前看起来如下所示:

models.py:

class RoomBooking(models.Model):
    room = models.ForeignKey(Room)
    bookedBy = models.ForeignKey(User)
    startTime = models.DateTimeField()
    stopTime = models.DateTimeField()
views.py:

@login_required
def report_company(request, year, month):
if request.user.is_staff:
    rooms_by_company = RoomBooking.objects.filter(startTime__year=year, startTime__month=month).order_by('bookedBy__userprofile__account')
    result = [list(v) for l, v in groupby(sorted(rooms_by_company, key=lambda x: x.bookedBy.userprofile.account.name), lambda x: x.bookedBy.userprofile.account.name)]

    if int(month) == 1:
        prev_year = int(year) - 1
        prev_month = 12
        next_year = int(year)
        next_month = int(month) + 1
    else:
        if int(month) == 12:
            prev_year = int(year)
            prev_month = int(month) - 1
            next_year = int(year) + 1
            next_month = 1
        else:
            prev_year = int(year)
            prev_month = int(month) - 1
            next_year = int(year)
            next_month = int(month) + 1

    return render_to_response(
        'roombooking_report.html',
        {
            'booking_list': result,
            'month_name': calendar.month_name[int(month)],
            'month': month,
            'year': year,
            'prev_month': prev_month,
            'prev_month_name': calendar.month_name[prev_month],
            'prev_year': prev_year,
            'next_month': next_month,
            'next_month_name': calendar.month_name[next_month],
            'next_year': next_year,
        },
        context_instance=RequestContext(request)
    )
else:
    t = get_template('403.html')
    context = RequestContext(request)
    return HttpResponse(t.render(context), status=403)
room_booking.html:

{% extends "base.html" %}

{% load url from future %}
{% block content %}
        <div>
            <h1>Room Booking Report for {{ month_name }} {{ year }}</h1>
            <a href="{% url 'meetingroombooking.views.report_company' year=prev_year month=prev_month %}">< {{ prev_month_name }} {{ prev_year }}</a> |
            <a href="{% url 'meetingroombooking.views.report_company' year=next_year month=next_month %}">{{ next_month_name }} {{ next_year }} ></a>
            <div>
            {% for company in booking_list %}
                <h2>{{ company.0.bookedBy.userprofile.account.name }}</h2>
                <ul>
                {% for b in company %}
                    <li>{{ b.bookedBy.first_name }} {{ b.bookedBy.last_name }} booked {{ b.room }} for {{ b.startTime }} to {{ b.stopTime }} ({{ b.startTime|timesince:b.stopTime }})</li>
                {% endfor %}
                </ul>
            {% endfor %}
            </ul>
            </div>
        </div>
{% endblock %}
我想在模板中添加该公司当月所有房间预订的总持续时间,但作为python新手,我不知道从哪里开始!我尝试过修改视图中分配给结果的列表,但无法编译它

如何查询数据库中的这些值,将其添加到结果中并在模板中显示