我在 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
任何想法?