如何为美味饼中的嵌套资源提供适当的URL [英] How to have proper urls for nested resources in tastypie

查看:55
本文介绍了如何为美味饼中的嵌套资源提供适当的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是来自Deliciouspie的嵌套资源食谱模式,可以在这里,只有我使用的是多对多关系.

I'm using the Nested Resource cookbook pattern from tastypie, which can be found here, only I'm using many-to-many relationships.

这意味着前缀URL,看起来像这样:

Which means the prepend urls, looks something like this:

class ParentResource(ModelResource):
children = fields.ToManyField(ChildResource, 'children')

def prepend_urls(self):
    return [
        url(r"^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)/childrens%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('get_children'), name="api_get_children"),
    ]

def get_children(self, request, **kwargs):
    #some way to get the filter
    child_resource = ChildResource()
    return child_resource.get_list(request, parent_id=obj.pk)

这很好用,除了分页使用子资源的URL而不是prepend__urls中的URL.IE.代替:

this works fine, except that pagination uses the child resource's url rather then the url in prepend__urls. I.E. instead of:

"meta": {
    "limit": 1,
    "next": "/api/parent/1/childrens?limit=1&offset=1",
    "offset": 0,
    "previous": null,
    "total_count": 2
},

我得到:

"meta": {
    "limit": 1,
    "next": "/api/parent/?limit=1&offset=1",
    "offset": 0,
    "previous": null,
    "total_count": 2
},

反正有没有使分页网址正确显示?

is there anyway t get the pagination urls to display properly?

推荐答案

我通过以下操作解决了此问题:

I fixed this doing following:

  1. 在子资源的get_list方法上使用"custom_uri"参数.为此,您必须重新实现完整方法:

def get_list(self, request, **kwargs):
    """
    Returns a serialized list of resources.

    Calls ``obj_get_list`` to provide the data, then handles that result
    set and serializes it.

    Should return a HttpResponse (200 OK).
    """
    # TODO: Uncached for now. Invalidation that works for everyone may be
    #       impossible.

    base_bundle = self.build_bundle(request=request)
    objects = self.obj_get_list(bundle=base_bundle, **self.remove_api_resource_names(kwargs))
    sorted_objects = self.apply_sorting(objects, options=request.GET)

    if 'custom_uri' in kwargs:
        resource_uri = kwargs['custom_uri']
    else:
        resource_uri = self.get_resource_uri()

    paginator = self._meta.paginator_class(request.GET, sorted_objects, resource_uri=resource_uri, limit=self._meta.limit, max_limit=self._meta.max_limit, collection_name=self._meta.collection_name)
    to_be_serialized = paginator.page()

    # Dehydrate the bundles in preparation for serialization.
    bundles = []

    for obj in to_be_serialized[self._meta.collection_name]:
        bundle = self.build_bundle(obj=obj, request=request)
        bundles.append(self.full_dehydrate(bundle, for_list=True))

    to_be_serialized[self._meta.collection_name] = bundles
    to_be_serialized = self.alter_list_data_to_serialize(request, to_be_serialized)
    return self.create_response(request, to_be_serialized)

  1. 在父方法上发送正确的uri:

def trip_photos(self, request, **kwargs):
    try:
        bundle = self.build_bundle(data={'pk': kwargs['pk']}, request=request)
        obj = self.cached_obj_get(bundle=bundle, self.remove_api_resource_names(kwargs))
    except ObjectDoesNotExist:
        return HttpGone()
    except MultipleObjectsReturned:
        return HttpMultipleChoices("More than one resource is found at this URI.")

    custom_uri = self._build_reverse_url('api_get_children', kwargs=self.resource_uri_kwargs(bundle))

    child_resource = ChildResource()
    return child_resource.get_list(request, parent_id=obj.pk, custom_uri=custom_uri)

这篇关于如何为美味饼中的嵌套资源提供适当的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆