Skip to content

Commit

Permalink
Add support for using custom serializer for serializing faceted objects
Browse files Browse the repository at this point in the history
  • Loading branch information
rhblind committed Jun 28, 2016
1 parent 0983e7b commit 257cfaa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions drf_haystack/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class FacetMixin(object):

facet_filter_backends = [HaystackFacetFilter]
facet_serializer_class = None
facet_objects_serializer_class = None

@list_route(methods=["get"], url_path="facets")
def facets(self, request):
Expand Down Expand Up @@ -101,3 +102,26 @@ def get_facet_serializer_class(self):
{"cls": self.__class__.__name__}
)
return self.facet_serializer_class

def get_facet_objects_serializer(self, *args, **kwargs):
"""
Return the serializer instance which should be used for
serializing faceted objects.
"""
assert "objects" in kwargs, "`objects` is a required argument to `get_facet_objects_serializer()`"

This comment has been minimized.

Copy link
@clintonb

clintonb Jun 28, 2016

Contributor

This condition is never met by the call below. This is a breaking change.


facet_objects_serializer_class = self.get_facet_objects_serializer_class()
kwargs["context"] = self.get_serializer_context()
kwargs["context"].update({
"objects": kwargs.pop("objects")
})
return facet_objects_serializer_class(*args, **kwargs)

def get_facet_objects_serializer_class(self):
"""
Return the class to use for serializing faceted objects.
Defaults to using the views ``self.serializer_class`` if not
``self.facet_objects_serializer_class`` is set.
"""
return self.facet_objects_serializer_class or super(FacetMixin, self).get_serializer_class()

2 changes: 1 addition & 1 deletion drf_haystack/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def get_objects(self, instance):

page = view.paginate_queryset(queryset)
if page is not None:
serializer = view.get_serializer(page, many=True)
serializer = view.get_facet_objects_serializer(page, many=True)
return OrderedDict([
("count", _get_count(queryset)),
("next", view.paginator.get_next_link()),
Expand Down

0 comments on commit 257cfaa

Please sign in to comment.