我在Django-Tastypie API上创建了自己的资源。 我一直在实现一些休息操作,现在我想实现我的自定义分页器,它将根据url传递的“key”生成next / prev值。
问题是:如果我传递一个像“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
有什么想法吗?
答案 0 :(得分:0)
我强烈建议您默认使用或自定义任何ModelResource
附带的分页器行为:http://django-tastypie.readthedocs.org/en/latest/paginator.html