Permalink
Browse files

Allow WebAPIResponsePaginator to take a custom object serializer.

There are times when a response needs to be able to serialize a list
using a custom serializer instead of the default registered ones. This
allows the caller to pass a function that will be used to serialize each
returned object in the list.
  • Loading branch information...
1 parent ef1a8e0 commit 6c9328002a1e78ea8c95dbe98b979fc0eaf009d9 @chipx86 chipx86 committed May 3, 2010
Showing with 9 additions and 1 deletion.
  1. +9 −1 djblets/webapi/core.py
View
10 djblets/webapi/core.py
@@ -278,6 +278,7 @@ def __init__(self, request, queryset, results_key="results",
prev_key="prev", next_key="next",
total_results_key="total_results",
default_max_results=25, max_results_cap=200,
+ serialize_object_func=None,
extra_data={}, *args, **kwargs):
try:
start = int(request.GET.get('start', 0))
@@ -291,7 +292,14 @@ def __init__(self, request, queryset, results_key="results",
except ValueError:
max_results = default_max_results
- results = list(queryset[start:start + max_results])
+ results = queryset[start:start + max_results]
+
+ if serialize_object_func:
+ results = [serialize_object_func(obj)
+ for obj in results]
+ else:
+ results = list(results)
+
total_results = queryset.count()
data = {

0 comments on commit 6c93280

Please sign in to comment.