django tsstypie。 order_by queryset

时间:2012-04-19 22:42:11

标签: django api sorting sql-order-by tastypie

这是我的models.py

class Picture(models.Model):
    image = models.ImageField(upload_to='uploads/')
    caption = models.CharField(max_length=140, null=True, blank=True)
    uploaded = models.DateField()
    comments = models.ManyToManyField(Comment, null=True, blank=True)

我的tastypie的API资源,api.py:

class PictureResource(ModelResource):

    class Meta:
        queryset = Picture.objects.all.order_by('-uploaded')
        resource_name = "photo"
        authorization = Authorization()
        API_LIMIT_PER_PAGE = 24

如您所见,我希望我的API页面按照上传的最新图片顺序排列。

我的错误代码是:

  

函数对象没有属性'order_by'

通常我不知道该怎么做......

(顺便说一下,API_LIMIT_PER_PAGE = 24是否在正确的位置?)

1 个答案:

答案 0 :(得分:5)

将您的queryset更改为:

class PictureResource(ModelResource):

    class Meta:
        queryset = Picture.objects.all().order_by('-uploaded')
        resource_name = "photo"
        authorization = Authorization()
        API_LIMIT_PER_PAGE = 24

all本身只是一个方法,但如果你像all()一样使用它,它会返回一个QuerySet。 order_by是一个QuerySet方法。

>> type(Picture.objects.all)
<type 'instancemethod'>

>> type(Picture.objects.all())
<class 'django.db.models.query.QuerySet'>