2012-09-07 17 views
5

如果有人閱讀Tastypie-Mailinglist:我沒有在那裏得到答案,所以很抱歉在這裏交叉發佈。在get_resource_uri中建立反向網址

在Tastypie中,我更改了資源的URL模式,因爲我使用了另一個鍵而不是PK。當我訪問資源時,這工作正常。 現在我想將此資源嵌套到父資源中,但嵌套資源包含帶有PK的URI,而不是我的自定義密鑰。我學到的是,就我而言,我必須改變孩子的get_resource_uri。

在我孩子的資源的方法(這是一個NamespacedResource)看起來是這樣的:

def get_resource_uri(self, bundle_or_obj): 

    obj = bundle_or_obj.obj if isinstance(bundle_or_obj, Bundle) else bundle_or_obj 

    kwargs={ 
     'resource_name': self._meta.resource_name, 
     'custom_id': obj.custom_id 
     } 

    return self._build_reverse_url('api_dispatch_detail', kwargs=kwargs) 

孩子的URL重寫方法是這樣的:

def override_urls(self): 
    return [ 
     url(r"^(?P<resource_name>%s)/(?P<custom_id>[-_\w\d]+)%s$" % (
       self._meta.resource_name, 
       trailing_slash() 
      ), 
      self.wrap_view('dispatch_detail'), 
      name="api_dispatch_detail" 
     ), 
    ] 

但是,應用程序不能顛倒的URL。我收到此錯誤:

Reverse for 'api_dispatch_detail' with arguments '()' and keyword arguments '{'custom_id': u'3_ee5-4423', 'resource_name': 'myresource'} not found. 

如何正確反轉URL?

在此先感謝。

回答

2

tasteypie的內部網址始終需要resource_nameapi_name kwargs。

你kwargs應包含:

kwargs = { 
    'api_name': 'v1', # Or whatever you have set for your api 
    'resource_name': self._meta.resource_name, 
    'custom_id': obj.custom_id 
} 
+0

完美,謝謝! – schneck