How to hydrate a resource_uri in django-tastypie?

梦想与她 提交于 2019-12-07 08:57:02

问题


I just have a Resource uri, like '/api/v1/myobjectresource/23/', and I need to hydrate it. It means, I want to instantiate MyObject(pk=23) from its resource_uri. How should I do this?


回答1:


Extracted from 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)


来源:https://stackoverflow.com/questions/11603754/how-to-hydrate-a-resource-uri-in-django-tastypie

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!