【问题标题】:subclassing Resource but uses 1 url for the resources in tastypie子类化资源,但使用 1 url 来存储美味派中的资源
【发布时间】:2013-02-01 14:58:02
【问题描述】:

假设我有 MediaResource 和两个相关(子类)资源,如下所示。

class PopularMediaResource(MediaResource):
 def get_object_list(self, request):
  return self.Meta.queryset.order_by('-num_hits')


class UserMediaResource(MediaResource):
 def get_object_list(self,request):
  return self.Meta.queryset.filter(mediahit__user__id=request.user.id)

(从https://groups.google.com/forum/?fromgroups=#!topic/django-tastypie/JGmY8tUzlX0借来的例子)
这很好用,但我必须为这些资源中的每一个使用不同的 URL

eg, /api/v1/MediaResource/
/api/v1/PopularMediaResource/
/api/v1/UserMediaResource/

等等……

所以having multiple resource classes 是当你想返回资源的different set 时使用tastepie 的典型方式?

【问题讨论】:

    标签: django rest tastypie


    【解决方案1】:

    我认为你应该使用过滤和排序机制。

    这是文档:

    http://django-tastypie.readthedocs.org/en/latest/resources.html#basic-filtering

    【讨论】:

    • 你能看看stackoverflow.com/questions/14645058/…吗?它归结为服务不同集合(过滤)资源的问题。 /api/v1/image/ /api/v1/image/user/{user_id} /api/v1/image/like/user/{user_id} 由于查询的复杂性,我认为没有办法通过 URL 参数为 /api/v1/image/like/user/{user_id} 提供过滤选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 2012-04-03
    • 2012-09-25
    相关资源
    最近更新 更多