【问题标题】:Django-Tastypie custom paginatorDjango-Tastypie 自定义分页器
【发布时间】:2013-03-09 23:42:24
【问题描述】:

我在我的 Django-Tastypie API 上创建了我自己的资源。 我一直在实现一些休息操作,现在我想实现我的自定义分页器,它将根据 url 传递的“键”生成下一个/上一个值。

问题是:如果我传递一个像“http:localhost:8000/api/stats/list/nextpage/”这样的值,obj_get 会被自动调用并且不让我返回一个值列表,只有一个值.

所以我希望 obj_get 返回多个值。 我的 api.py 看起来像这样:

我的资源类:

class CountryCountResource(Resource):

   countryName= fields.CharField(attribute='countryName')
   countryCount = fields.FloatField(attribute='countryCount')

   class Meta:
       resource_name = 'country/list'
       object_class = dict2obj
       include_resource_uri = False
       authorization = DjangoAuthorization()
       authentication = BasicAuthentication()

   def detail_uri_kwargs(self, bundle_or_obj):
       kwargs = {}

       if isinstance(bundle_or_obj, Bundle):
           kwargs['pk'] = bundle_or_obj.obj.countryName
       else:
           kwargs['pk'] = bundle_or_obj.countryName

       return kwargs

   def obj_get_list(self, request=None, **kwargs):

       db = MySQLdb.connect(host='xxxxxx',user='xxx',passwd='xxx',db='xxx')
       cur = db.cursor()
       cur.execute("SELECT count(*) FROM users")
       total_users=cur.fetchall()
       for item in total_users:
           totalint=int(item[0])

       cur.execute("SELECT country,count(country) FROM users GROUP BY country ORDER BY    count(country) DESC LIMIT 0,10")

       #ordered tuple
       mylist=cur.fetchall()

       newlist=[]
       for i in mylist:
           auxd={}
           auxd['countryName']=str(i[0])
           res=int(i[1])/float(totalint)
           res="%.2f" % res
           auxd['countryCount']=res
           newlist.append(dict2obj(auxd))

       db.close()

       return newlist

   def obj_get(self, request=None, **kwargs):
       return newlist <<<<<<------- DONT WORK

有什么想法吗?

【问题讨论】:

    标签: django rest pagination tastypie


    【解决方案1】:

    我强烈建议默认使用或自定义任何ModelResource 附带的分页器行为:http://django-tastypie.readthedocs.org/en/latest/paginator.html

    【讨论】:

      猜你喜欢
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 2011-11-13
      • 1970-01-01
      相关资源
      最近更新 更多