django-查询按时间排序

Meta类实现

class News(models.Model):
    title = models.CharField(max_length=35)
    desc = models.CharField(max_length=100)
    thumbnail = models.URLField()
    content = models.TextField()
    pubtime = models.DateTimeField(auto_now_add=True)
    category = models.ForeignKey(NewsCategory, on_delete=models.SET_NULL, null=True)
    author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)

    class Meta:
        ordering = ['-pubtime']  # 以后进行News.object提取数据时,按照指定字段的排序提取数据

猜你喜欢

转载自www.cnblogs.com/tangpg/p/9300841.html