1

我有下面的代码似乎工作正常,除了将requestand设置kwargs为空get_object_list

class Target(object):

    def __init__(self, x=None, y=None):
        self.id = 1 #Since tastypie needs an id
        self.x = x
        self.y = y



class TargetResource(Resource):
    x = fields.IntegerField(attribute='x')
    y = fields.IntegerField(attribute='y')

    def get_object_list(self, request=None, **kwargs):
        targets = []
        print 'Got Request %s kwargs %s' %(request, kwargs) 
        target = self.obj_get(request, **kwargs)
        targets.append(target)

        return targets

    def obj_get(self, request=None, **kwargs):
       #Do something that requires the request and kwargs
       #Return an instance of Target


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

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

        return kwargs

    def obj_get_list(self, request=None, **kwargs):
        # Filtering disabled for brevity...
        return self.get_object_list(request)


    class Meta:
        resource_name = 'target'
        allowed_methods = ['get']
        object_class = Target
        authorization = DjangoAuthorization()
        authentication = MultiAuthentication(BasicAuthentication(), SessionAuthentication())
4

2 回答 2

3

obj_get_listget_list( tastepie/resources.py )调用

如果你看一下get_list代码:

def get_list(self, request, **kwargs):
        base_bundle = self.build_bundle(request=request)
        objects = self.obj_get_list(bundle=base_bundle, *self.remove_api_resource_names(kwargs))
        ...

您可以看到请求以捆绑包的形式传递给obj_get_list.

因此,您应该执行以下obj_get_list操作来获取请求:

def obj_get_list(self, request=None, **kwargs):
    return self.get_object_list(kwargs['bundle'].request)
于 2013-06-29T10:41:08.813 回答
1

**kwargs没有填充,因为正常不get_object_list()接受它们。来自tastepie/resources.pydef get_object_list(self, request): 至于request,类似:

def get_object_list(self, request):
    if request is None:
        import pdb
        pdb.set_trace()
    # the rest of your get_object_list() code

应该让你进入调试器,让你找出哪些代码没有传递请求。

于 2013-06-19T23:49:22.207 回答