Python 按日期对所有信息进行排序DJANGO

Python 按日期对所有信息进行排序DJANGO,python,django,web-applications,backend,Python,Django,Web Applications,Backend,我有一个名为“Thing”的模型,正如HelloWebApp书中所述。现在我添加了一个“重量”模型。该模型有“日期”和“权重值”字段。在“thing_detail.html”中显示数据时,我想显示按字段“date”排序的数据 这是我的模特 from django.db import models from django.contrib.auth.models import User from django.db import models from django.utils import tim

我有一个名为“Thing”的模型,正如HelloWebApp书中所述。现在我添加了一个“重量”模型。该模型有“日期”和“权重值”字段。在“thing_detail.html”中显示数据时,我想显示按字段“date”排序的数据

这是我的模特

from django.db import models
from django.contrib.auth.models import User
from django.db import models
from django.utils import timezone

class Thing(models.Model):
    name = models.CharField(max_length=255)
    description = models.TextField()
    slug = models.SlugField(unique=True)
    user = models.OneToOneField(User, blank=True, null=True)

class Weight(models.Model):
    date = models.DateTimeField(default=timezone.now)
    weight_value = models.CharField(max_length=255)
    thingRA = models.ForeignKey(Thing,related_name="weights")

    class Meta:
        order_with_respect_to = 'thingRA'
还有thing_details.html

{% extends 'layouts/base.html' %}
{% block title %}
{{ thing.name }} - {{ block.super }}
{% endblock %}
{% block content %}
<div id="content">
<!-- Below is to check if the data belongs to the original user -->
{% if user == thing.user %}
<h1>{{ thing.name }}</h1>
<p>{{ thing.description }}</p><br>
{% if weights %}
    {% for weightshow in weights %}
        <p>>Weight as on {{weightshow.date}} : {{weightshow.weight_value}}</p>
    {% empty %}
        <p>Sorry! You haven't logged any weight records yet</p>
   {% endfor %}
{% endif %}
<br><br>
<a href="{% url 'edit_thing' slug=thing.slug %}"><u>Edit Me!</u></a>
<br>
<a href="{% url 'edit_weight' slug=thing.slug %}"><u>Log Weight Record!</u></a>
</div>
{% else %}
<h1>Error!</h1>
<p>You do not have the permission to view this account. If this is your account please logout and login to <strong>{{thing.name}}</strong></p><br>
{% endif %}
{% endblock %}
{%extends'布局/base.html%}
{%block title%}
{{thing.name}}-{{block.super}
{%endblock%}
{%block content%}
{%if user==thing.user%}
{{thing.name}
{{thing.description}}


{%if权重%} {weightshow在weights%中的百分比} >在{{weightshow.date}}上的权重:{{weightshow.Weight_value}

{%empty%} 对不起!你还没有记录任何体重记录

{%endfor%} {%endif%}


{%else%} 错误! 您没有查看此帐户的权限。如果这是您的帐户,请注销并登录到{{thing.name}


{%endif%} {%endblock%}
请有人帮忙


谢谢

将此添加到您的体重模型中

class Meta:
    ordering = ['date']
或者,在进行查询时,请按()添加订单。像


感谢编辑@kartikmajiThanks!工作完美。
Weight.objects.all().order_by('date')