0

我正在使用来自美味派的嵌套资源食谱模式,可以在这里找到,只是我使用的是多对多关系。

这意味着前置 url,看起来像这样:

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而不是:

"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
},

无论如何都无法正确显示分页网址?

4

2 回答 2

2

我解决了这个问题:

  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)
于 2015-03-11T16:36:26.560 回答
0

我认为应该改变的是近似值。ChildResource你为什么不直接从

http://SERVER/api/v1/child/?parent=1&limit=1&offset=1

那么分页没有问题。

于 2013-09-10T10:04:39.913 回答