1

这里是 Python 新手......我的 Django 模型中有一堆RoomBooking事件的开始和停止时间,我需要检索一个月内每个事件的总持续时间,按公司分组。目前看起来如下:

模型.py:

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

视图.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 %}

我想<h2>在模板中的标签中添加该公司当月所有房间预订的总持续时间,但作为 python 新手,我不知道从哪里开始!我已经尝试更改在视图中分配给的列表result,但我无法编译它。

我将如何在数据库中查询这些值,将其添加到result模板中并在模板中显示?

4

0 回答 0