【问题标题】:django tsstypie. order_by querysetdjango tsstypie。 order_by 查询集
【发布时间】:2012-04-19 22:42:11
【问题描述】:

这是我的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)

还有我的 sweetpie 的 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 来对地方了吗?)

【问题讨论】:

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


    【解决方案1】:

    将您的 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() 一样使用它,它会返回一个查询集。 order_by 是一个 QuerySet 方法。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 2023-01-22
      • 2019-12-02
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多