问题
I'm using the Nested Resource cookbook pattern from tastypie, which can be found here, only I'm using many-to-many relationships.
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)
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
},
I get:
"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?
回答1:
I fixed this doing following:
- Use a "custom_uri" argument on the get_list method of the child resource. To do that you have to reimplement the full method:
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)
- Send the correct uri on the parent method:
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)
回答2:
I think it is the approximation that should be changed. Why don't you just get the children ( children goes without 's', already plural ;) ) related to a parent from the ChildResource
?
http://SERVER/api/v1/child/?parent=1&limit=1&offset=1
No problems with pagination then.
来源:https://stackoverflow.com/questions/18692364/how-to-have-proper-urls-for-nested-resources-in-tastypie