Hey guys
I’m new
I want to create the crud search for the app like search name input
How I do the search using viewsets
Fundamentally it’s really not any different.
If you think about the “traditional” Django way of working, you have a view that accepts parameters as HTML form data, and renders HTML as the response.
Using the Django Rest Framework doesn’t change that fundamental flow - the only difference is that you’re almost always accepting JSON as the input and rendering JSON as the output. Whether you’re doing a search or rendering an object doesn’t matter.
Ken
Look into Django-filter
( https://django-filter.readthedocs.io/en/stable/ ) and use query parameters.
Hey Ken and JonasKs thanks for update
I come from another language.
I’m new in python I have this course but I need help
I create the route
‘’’
from django.urls import path, include
from rest_framework import routers
from .views import TenantViewSet, TenantSearch
router = routers.DefaultRouter()
router.register(’’, TenantViewSet)
router.register(‘list’, TenantSearch)
urlpatterns = [
path(’’, include(router.urls)),
]
‘’’
on my serailizer I have
‘’’
from rest_framework import serializers
from .models import Tenant
class TenantSerializers (serializers.ModelSerializer):
class Meta:
model = Tenant
#fields = [‘firstName’,‘lastName’, ‘phone’]
fields = ‘all’
‘’’
this is my view
from rest_framework import viewsets
from .serializers import TenantSerializers
from .models import Tenant
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated, AllowAny
from rest_framework.generics import ListAPIView
from rest_framework.filters import SearchFilter, OrderingFilter
class TenantViewSet(viewsets.ModelViewSet):
serializer_class = TenantSerializers
queryset = Tenant.objects.all()
authentication_classes = (TokenAuthentication,)
#permission_classes = (IsAuthenticated,)
permission_classes = (AllowAny,)
class TenantSearch(ListAPIView):
serializer_class = TenantSerializers
queryset = Tenant.objects.all()
authentication_classes = (TokenAuthentication,)
# permission_classes = (IsAuthenticated,)
permission_classes = (AllowAny)
filter_backends = (SearchFilter, OrderingFilter)
search_fields = (‘firstName’)
my URL no working
http://127.0.0.1:8000/api/tenant/list?search=asdas
I need the creat the search on the CRUD. My insert updates and delete it is working.
First, I suggest that in the future that when you’re posting code in this forum, that you enclose your code between two lines consisting only of three backtick (`) characters. That way your code maintains its formatting, making it easier to read.
Now, what I’m seeing in a couple of cases is that you’re defining some fields that are supposed to be tuples - but are missing the trailing comma.
example, you have:
search_fields = (‘firstName’)
when it really should be:
search_fields = (‘firstName’,)
Or, if you want to avoid this type of error in the future, just go ahead and make it a list:
search_fields = [‘firstName’]
(There really is no benefit here to making it a tuple rather than a list, and it avoids that error completely.)
Ken