Django:按datetime字段排序只精确到分钟,而不是秒?

2024-10-08 19:55:05 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有办法让Django考虑通过DateTime字段对queryset进行秒(甚至毫秒)排序?你知道吗

我想显示一个表格,上面按时间倒序排列我的血液检测结果。只要测试不是在同一分钟内添加的,它就可以工作。如果它们仅以秒分隔,则按时间顺序列出该分钟的持续时间。你知道吗

models.py
class BloodTest(models.Model):
    patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
    name = models.CharField(max_length=20, choices=BLOOD_TEST_CHOICES, 
        default=BILIRUBIN)
    value = models.PositiveSmallIntegerField(max_length=10)        
    time = models.DateTimeField(default=timezone.now(), validators= 
        [validate_date])

views.py
def blood_list(request, blood_id):
    blood = BloodTest.objects.get(id=blood_id)
    patient = blood.patient
    blood_list = BloodTest.objects.filter(name=blood.name).filter(patient=patient.id).order_by('-time')
    context = {'blood_list': blood_list, 'patient': patient, 'blood': blood}
    return render(request, 'vitals/blood-list.html', context)

blood-list.html template
{% for blood in blood_list %}
    <tr>
      <td>{{ blood.time }}</td>
      <td><a href="{% url 'blood' blood.id %}">{{ blood.value }}</a></td>
    </tr>
  {% endfor %}

Tags: namepyiddefaulttimevaluemodels时间

热门问题