按日期对所有信息进行排序 DJANGO
Sort All the Information by Date DJANGO
我有一个名为 "Thing" 的模型,如 HelloWebApp 书中所述。现在我添加了一个 "weight" 模型。该模型具有字段 "date" 和 "weight_value"。在显示 "thing_detail.html" 中的数据时,我想显示按字段 "date".
排序的数据
这是我的 models.py
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 %}
好心人帮忙!
谢谢!
将此添加到您的体重模型中
class Meta:
ordering = ['date']
或者当您进行查询时添加 order_by()。喜欢,
Weight.objects.all().order_by('date')
我有一个名为 "Thing" 的模型,如 HelloWebApp 书中所述。现在我添加了一个 "weight" 模型。该模型具有字段 "date" 和 "weight_value"。在显示 "thing_detail.html" 中的数据时,我想显示按字段 "date".
排序的数据这是我的 models.py
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 %}
好心人帮忙!
谢谢!
将此添加到您的体重模型中
class Meta:
ordering = ['date']
或者当您进行查询时添加 order_by()。喜欢,
Weight.objects.all().order_by('date')