元类下的tastypie访问请求用户名

时间:2014-02-04 07:01:17

标签: python tastypie

我有一个tastypie资源类,如下所示

class ItemsResource(ModelResource):

    taxclass_id=fields.CharField(attribute='taxclass_id_id')
    parent_id=fields.CharField(attribute='parent_id_id')
    branch_id=fields.CharField(attribute='branch_id_id')
    isactive=fields.CharField(attribute='isactive_id')
    metric=fields.CharField(attribute='metric')
    class Meta:
        username=<<how to access request.username here!>>
        queryset = Item.objects.filter(username=username)
        allowed_methods = ['get']
        authentication = ApiKeyAuthentication()

我想根据请求用户名过滤queryset,如何在Meta类中访问request.username

1 个答案:

答案 0 :(得分:1)

我认为可以通过覆盖get_object_list方法

来实现相同的过滤
    def get_object_list(self, request):
        username=request.GET['username']
        return super(ItemsResource, self).get_object_list(request).filter(username=username) 
相关问题