I need some guidance with finishing my customized api endpoints for my search query in my Django views
I’m trying to finish customizing the search_query_set
for ArboristCompany in my Django views. I’m following the Django-Haystack docs.
I’m also using DRF-Haystack. In the docs, it shows you how to set up the search_querty_set
views with template files. However, I already have a VueJS search page API call.
Basically, I want to know how to implement my VueJS search page API call with the search_query_set
API endpoint in my views, like I did with the the VueJS API calls for the Login/Register API endpoints
Here are some files that may be of some use to you. serializers.py
class CompanySerializer(HaystackSerializer):
class Meta:
index_class = [ArboristCompanyIndex]
fields = [
'text', 'company_name', 'company_city', 'company_state'
]
search_indexes.py
class ArboristCompanyIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.EdgeNgramField(document=True, use_template=True)
company_name = indexes.CharField(model_attr='company_name')
company_city = indexes.CharField(model_attr='company_city')
company_state = indexes.CharField(model_attr='company_state')
# Instead of directly indexing company_price, use the price_display method
company_price_display = indexes.CharField(model_attr='price_display', null=True) # Using the custom price_display method
experience = indexes.CharField(model_attr='experience')
def get_model(self):
return ArboristCompany
def index_queryset(self, using=None):
return self.get_model().objects.filter(content='foo').order_by('company_name', 'company_city', 'company_state', 'experience')
views
@authentication_classes([JWTAuthentication])
class LoginView(APIView):
def post(self, request, *args, **kwargs):
serializer = LoginSerializers(data=request.data, context={'request': request})
serializer.is_valid(raise_exception=True)
user = serializer.validated_data['user']
login(None, user)
token = Token.objects.create(user=user)
return Response({"status": status.HTTP_200_OK, "Token": token.key})
if user is not None:
# Generate token
refresh = RefreshToken.for_user(user)
access_token = str(refresh.access_token)
return Response({
'message': 'Successful login',
'access_token': access_token,
'refresh_token': str(refresh),
}, status=status.HTTP_200_OK)
return Response({'error': 'Invalid email/password'}, status=status.HTTP_401_UNAUTHORIZED)
class RegisterView(APIView):
def post(self, request):
serializer = RegisterSerializer(data=request.data, context={'request': request})
if serializer.is_valid():
serializer.save()
return Response({
'message': 'successfully registered',
}, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class CompanySearchView(HaystackSerializer):
class Meta:
index_class = [ArboristCompanyIndex]
fields = [
'text', 'company_name', 'company_city', 'company_state'
]
def search_company(request):
index_classes = [ArboristCompany]
serializer_class = CompanySerializer
query = request.GET.get('q')
queryset = SearchQuerySet.filter(content='foo').order_by('company_name', 'company_city', 'company_state', 'experience')
And finally urls.py (app)
from django.urls import path
from arborfindr.views import index
from .views import homeowner_info, review_info, services_info
from . import views
from django.contrib.auth import views as auth_views
from django.conf import settings
from django.conf.urls.static import static
from .views import RegisterView, LoginView
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView
from rest_framework import routers
app_name = 'arborfindr'
#router = routers.DefaultRouter
#router.register('company/search', CompanySearchView, base_name='company-search')
urlpatterns = [
path('register/', RegisterView.as_view(), name='register'), # Register API endpoint url
path('login/', LoginView.as_view(), name ='login'), # Login API endpoint url
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('update_password/', views.update_password, name='update_password'),
path('homeowner_dashboard/', views.homeowner_dashboard, name='homeowner_dashboard'),
path('company_profile/', views.company_profile, name='company_profile'),
path('edit_homeowner_dashboard/', views.edit_homeowner_dashboard, name='edit_homeowner_dashboard'),
path('edit_company_profile/', views.edit_company_profile, name='edit_company_profile'),
path('arbor/review/', views.arbor_review, name='arbor_review'),
path('homeowner/', homeowner_info),
path('review_arbor/', review_info),
path('services/', services_info),
path('token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('arborfindr/', index),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)