2013-06-19 43 views
1

我有下面的代碼,似乎除了具有requestkwargs爲空做工精細的get_object_list請求是沒有在非ORM Tastypie資源流動

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()) 

回答

1

**kwargs沒有填充因爲正常get_object_list()不接受它們。從tastypie/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 

應該放棄你進入調試器,讓你找出什麼代碼不流通請求

3

obj_get_list被稱爲get_listtastypie/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)