首页 > 解决方案 > 如何在 Django views.py 的查询集中将持续时间或字符串转换为整数?

问题描述

我需要计算两行之间的时间差。如果总数为 0,则此持续时间需要调整为 1,或者如果总数超过 15 分钟,则调整为 0——我打算把这个更正的数量放在 strtime 中。不幸的是,我计算的时间不正确,我也无法将提取的分钟数转换为整数以测试持续时间是否 > 15 或 < 1。我转换为字符串的唯一原因是查看是否可以转换为整数——它不起作用。我还尝试转换为 pandas 数据框以进行计算,但我无法将其恢复为某种格式以在 html 页面上正确显示。

如何在行之间进行准确计算,如何从 0 调整到 1 或 > 15 到 0?我尝试了许多不同的版本——只是运气不佳。我通常使用 javascript 工作——这是我的第一个 Django 项目。我已经清理并缩短了数据。此页面将适合现有的 Django 设置。先感谢您!代码如下。

models.py
class testmodels(models.Model):
    primary_key_id = models.CharField(max_length=150, primary_key=True)
    transactionDate = models.DateTimeField(blank=True, null=True)
    userName = models.CharField(max_length=150, blank=True)
    code = models.CharField(max_length=100, blank=True)

views.py
    items = testmodels.objects.filter(transaction_type='Pick').order_by('transactionDate')).values()
    items2 = items.annotate(totaltime=Window(expression=Lead('transactionDate')) - F('transactionDate'))
    items3 = items2.annotate(dtime=ExtractMinute(ExpressionWrapper(F('totaltime'),output_field=DurationField())))
    items4 = items3.annotate(strtime=(ExpressionWrapper(F(str('dtime')),output_field=CharField())))
    context = { 'itemdetails':items4}
    return render(request, 'test.html',context=items4)

test.html
  {% for row in itemdetails.all %}
  <tr>
    <td> {{ row.transactionDate }} </td>
    <td> {{ row.userName }} </td>
    <td> {{ row.totaltime }} </td>
    <td> {{ row.dtime }} </td>
    <td> {{ row.strtime }} </td>
  </tr>
  {% endif %}

Data:
    Transaction Date            User    Total Time  dtime   strtime 
    Jan. 11, 2019, 3:20 p.m.    Bob     0:00:28     0   0.0
    Jan. 11, 2019, 3:20 p.m.    Bob     0:01:18     1   1.0
    Jan. 11, 2019, 3:21 p.m.    Bob     0:00:41     0   0.0
    Jan. 11, 2019, 3:22 p.m.    Bob     0:23:50     23  23.0
    Jan. 11, 2019, 3:24 p.m.    Bob     -1 day, 23:55:59    -4  -4.0
    Jan. 11, 2019, 3:25 p.m.    Bob     0:00:41     0   0.0
    Jan. 11, 2019, 3:26 p.m.    Bob     0:00:04     0   0.0
    Jan. 11, 2019, 3:26 p.m.    Bob     4:41:16     41  41.0    
    Jan. 11, 2019, 3:46 p.m.    Bob     0:00:10     0   0
    Jan. 11, 2019, 3:46 p.m.    Bob     0:00:52     0   0

标签: djangodjango-templatesdjango-views

解决方案


我正在努力理解您想要的确切舍入方案,但我认为这将为您提供您正在寻找的结构:

为绝对值创建了一个自定义函数。这适用于 Postgres,您必须确保它适用于您的数据库。

functions.py

from django.db.models import Func

class ABS(BaseFunc):
    function = "ABS"

views.py
items = (testmodels
         .objects
         .filter(transaction_type='Pick')
         .order_by('transactionDate')
         .annotate(current_dtime=F('transactionDate'))
         .annotate(previous_dtime=Window(expression=Lead('transactionDate')))
         .annotate(diff_dtime=F('current_dtime') - F('previous_dtime'))
         .annotate(diff_minute=ABS(ExtractMinute('diff_dtime', output_field=IntegerField())))
         .annotate(rounding_scheme=Case(When(diff_minute=0, then=Value(0)),
                                        When(diff_minute__lt=15, then=Value(1)),
                                        default=Value(0),
                                        output_field=IntegerField())))

推荐阅读