In most cases, We use Django Rest Framework filtering module to filter data but if you have large dataset then in those cases we must need indexing engine to index data and then do filtering on them. There are many search indexing engines out there. In this article we will use one of them and will understand how that can be used in Django project along with Django Rest Framework.
All index engines are differently implemented and to use them in python they are providing interfaces. Django Haystack is the python package which provides the bridge between Django and Indexing engines.
Following Search Engines can be used to implement search API in Django Project.
Start New Project
To start new project please look at this article ‘Create new project with django > 2’
Install Required additional packages
pip install Django-haystack
pip install djangorestframework
pip install whoosh
After updating settings, we are ready to generate index and use them but what must be indexed that still to be implemented. So, let’s go further and write code to be indexed.
First, start new app called Products
python mange.py startapp products
Write product model in models.py
Now write search_indexes.py in products app as following.
After writing above Index class, you will be able to build index using following command. But to see real indexed data we must add few product and to do that we will register product model in admin and then will add few objects from there.
If you haven’t created superuser, Please create superuser using following command.
python manage.py createsuperuser
Now run server and then create few objects from admin panel with superuser. Objects are now available in database for product model so let’s build index for the model data. To build index, use following command
python manage.py update_index
You will see count of objects which are indexed in console output of above command. Further we need to write code to access those indexed data through REST API. To do that, we need to write one REST API using DRF power.
We have to write one serializer class so, create one serializers.py in products app and add following code.
Please remember that, only those fields can be added here which are considered in index class. Though you can exclude few fields if you don’t need in search results. If you need only id and title then remove other lines from this class.
Further let’s write required view and then url -
Add this into root URLs where settings.py is available
Search API URL can be set anything you need and then instead of using routers you can apply viewsets this way.
Now you are ready to see the feature in action. Run the server and hit following URL in browser.
You should be able to see all created products and if you want to search then pass `?q=`
Thanks for reading this article and I hope you have understood how to use haystack with Django rest framework.
Originally published at http://www.aaravtech.com.