| 1 |
.. _tips-n-tricks-label: |
| 2 |
|
| 3 |
Tips'n Tricks |
| 4 |
============= |
| 5 |
|
| 6 |
Reusing Model serializers |
| 7 |
------------------------- |
| 8 |
|
| 9 |
It may be useful to be able to use existing model serializers to return data from search requests in the same format |
| 10 |
as used elsewhere in your API. This can be done by modifying the ``to_representation`` method of your serializer to |
| 11 |
use the ``instance.object`` instead of the search result instance. As a convenience, a mixin class is provided that |
| 12 |
does just that. |
| 13 |
|
| 14 |
.. class:: drf_haystack.serializers.HaystackSerializerMixin |
| 15 |
|
| 16 |
An example using the mixin might look like the following: |
| 17 |
|
| 18 |
.. code-block:: python |
| 19 |
|
| 20 |
class PersonSerializer(serializers.ModelSerializer): |
| 21 |
class Meta: |
| 22 |
model = Person |
| 23 |
fields = ("id", "firstname", "lastname") |
| 24 |
|
| 25 |
class PersonSearchSerializer(HaystackSerializerMixin, PersonSerializer): |
| 26 |
class Meta(PersonSerializer.Meta): |
| 27 |
search_fields = ("text", ) |
| 28 |
|
| 29 |
The results from a search would then contain the fields from the ``PersonSerializer`` rather than fields from the |
| 30 |
search index. |
| 31 |
|
| 32 |
.. note:: |
| 33 |
|
| 34 |
If your model serializer specifies a ``fields`` attribute in its Meta class, then the search serializer must |
| 35 |
specify a ``search_fields`` attribute in its Meta class if you intend to search on any search index fields |
| 36 |
that are not in the model serializer fields (e.g. 'text') |
| 37 |
|
| 38 |
.. warning:: |
| 39 |
|
| 40 |
It should be noted that doing this will retrieve the underlying object which means a database hit. Thus, it will |
| 41 |
not be as performant as only retrieving data from the search index. If performance is a concern, it would be |
| 42 |
better to recreate the desired data structure and store it in the search index. |
| 43 |
|
| 44 |
|
| 45 |
Regular Search View |
| 46 |
------------------- |
| 47 |
|
| 48 |
Sometimes you might not need all the bells and whistles of a ``ViewSet``, |
| 49 |
but can do with a regular view. In such scenario you could do something like this. |
| 50 |
|
| 51 |
.. code-block:: python |
| 52 |
|
| 53 |
# |
| 54 |
# views.py |
| 55 |
# |
| 56 |
|
| 57 |
from rest_framework.mixins import ListModelMixin |
| 58 |
from drf_haystack.generics import HaystackGenericAPIView |
| 59 |
|
| 60 |
|
| 61 |
class SearchView(ListModelMixin, HaystackGenericAPIView): |
| 62 |
|
| 63 |
serializer_class = LocationSerializer |
| 64 |
|
| 65 |
def get(self, request, *args, **kwargs): |
| 66 |
return self.list(request, *args, **kwargs) |
| 67 |
|
| 68 |
|
| 69 |
# |
| 70 |
# urls.py |
| 71 |
# |
| 72 |
|
| 73 |
urlpatterns = ( |
| 74 |
... |
| 75 |
url(r'^search/', SearchView.as_view()), |
| 76 |
... |
| 77 |
) |
| 78 |
|
| 79 |
You can also use `FacetMixin` or `MoreLikeThisMixin` in your regular views as well. |
| 80 |
|
| 81 |
.. code-block:: python |
| 82 |
|
| 83 |
# |
| 84 |
# views.py |
| 85 |
# |
| 86 |
|
| 87 |
from rest_framework.mixins import ListModelMixin |
| 88 |
from drf_haystack.mixins import FacetMixin |
| 89 |
from drf_haystack.generics import HaystackGenericAPIView |
| 90 |
|
| 91 |
|
| 92 |
class SearchView(ListModelMixin, FacetMixin, HaystackGenericAPIView): |
| 93 |
index_models = [Project] |
| 94 |
serializer_class = ProjectListSerializer |
| 95 |
facet_serializer_class = ProjectListFacetSerializer |
| 96 |
|
| 97 |
pagination_class = BasicPagination |
| 98 |
permission_classes = (AllowAny,) |
| 99 |
|
| 100 |
def get(self, request, *args, **kwargs): |
| 101 |
return self.facets(request, *args, **kwargs) |