Django tastypie高级过滤

时间:2013-03-28 15:15:04

标签: python django filter tastypie

我正在尝试在我的资源中实现高级过滤器,但我需要一些帮助来构建和应用我自己的过滤器。 目标是在我的api UserResource中获取过滤器信息,然后通过这样的URL获取对象列表:

http://127.0.0.1:8000/api/users/list/&orderby="gender"&limit=10

实际上,我的资源看起来像这样:

class UserResource(Resource):

   firstName = fields.CharField(attribute='firstName')
   lastName = fields.CharField(attribute='lastName')
   gender= fields.CharField(attribute='gender')
   status= fields.IntegerField(attribute='status')
   date= fields.DateTimeField(attribute='date')

   class Meta:
       resource_name = 'users/list'
       object_class = dict2obj
       authorization = Authorization()

   def detail_uri_kwargs(self, bundle_or_obj):
       kwargs = {}
       if isinstance(bundle_or_obj, Bundle):
           kwargs['pk'] = bundle_or_obj.obj.firstName
       else:
           kwargs['pk'] = bundle_or_obj.firstName
       return kwargs

   def obj_get_list(self, request=None, **kwargs):
       db = MySQLdb.connect(host='xxx.xxx.xxx',user='test2',passwd='xxx',db='mydb')
       cur = db.cursor()
       cur.execute("SELECT * FROM users order by gender Desc limit 0,10")
       users=cur.fetchall()
       #...
       db.close()
       return posts

我想知道如何获取过滤器信息以返回我的obj列表。

0 个答案:

没有答案