4

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

4

1 回答 1

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)
于 2013-01-04T13:22:13.703 回答