如何在django-tastypie中补充resource_uri?

时间:2012-07-22 20:26:54

标签: django tastypie

我只有一个资源uri,比如'/api/v1/myobjectresource/23/',我需要补充它。 这意味着,我想从resource_uri实例化MyObject(pk = 23)。我该怎么做?

1 个答案:

答案 0 :(得分:4)

摘自https://gist.github.com/794424

  

def get_resource_uri(self,bundle_or_obj):

kwargs = {
    'resource_name': self._meta.resource_name,
}
if isinstance(bundle_or_obj, Bundle):
    kwargs['pk'] = bundle_or_obj.obj.id # pk is referenced in ModelResource
else:
    kwargs['pk'] = bundle_or_obj.id
if self._meta.api_name is not None:
    kwargs['api_name'] = self._meta.api_name
return self._build_reverse_url('api_dispatch_detail', kwargs = kwargs)