我使用的是python2.7,django1.8.4,MySQL。
我尝试过以下方法: 的观点:
messages=Message.objects.all().order_by('time').reverse()
messages=Message.objects.all().order_by('-time')
模型:
class Message(models.Model):
title=models.CharField(max_length=100,blank=True)
body=models.TextField(blank=True)
time=models.DateField(timezone.now,blank=True,null=True)
def __unicode__(self):
return smart_unicode(self.title)
我使用django shell来测试:
>>> from main.models import Message
>>> messages=Message.objects.all()
>>> messages
[<Message: 7ht>, <Message: >, <Message: fvdsv>, <Message: dddddddd>, <Message: dddddddd>, <Message: dddddddd>, <Message: sssssssssss>, <Message: >, <Message: >, <Message: >, <Message: >, <Message: sssssssssss>, <Message: sssssssssss>, <Message: sssssssssss>, <Message: sssssssssss>, <Message: sssssssssss>]
>>> messages=Message.objects.order_by("time")
>>> messages
[<Message: 7ht>, <Message: >, <Message: fvdsv>, <Message: dddddddd>, <Message: dddddddd>, <Message: dddddddd>, <Message: sssssssssss>, <Message: >, <Message: >, <Message: >, <Message: >, <Message: sssssssssss>, <Message: sssssssssss>, <Message: sssssssssss>, <Message: sssssssssss>, <Message: sssssssssss>]
>>> messages.reverse()
[<Message: 7ht>, <Message: >, <Message: fvdsv>, <Message: dddddddd>, <Message: dddddddd>, <Message: dddddddd>, <Message: sssssssssss>, <Message: >, <Message: >, <Message: >, <Message: >, <Message: sssssssssss>, <Message: sssssssssss>, <Message: sssssssssss>, <Message: sssssssssss>, <Message: sssssssssss>]
答案 0 :(得分:0)
time=models.DateField(timezone.now,blank=True,null=True)
应该是:
time=models.DateTimeField(timezone.now,blank=True,null=True)