Skip to content

Instantly share code, notes, and snippets.

@mansha99
Created July 7, 2023 07:15
Show Gist options
  • Save mansha99/8ca3689ff328db72dcd489af7217da73 to your computer and use it in GitHub Desktop.
Save mansha99/8ca3689ff328db72dcd489af7217da73 to your computer and use it in GitHub Desktop.
notices/views.py
from .models import Notice
from .serializers import NoticeSerializer
from rest_framework import viewsets
from .pagination import CustomPagination
from rest_framework.exceptions import ValidationError
class NoticeModelViewSet(viewsets.ModelViewSet):
queryset = Notice.objects.all()
serializer_class = NoticeSerializer
pagination_class = CustomPagination
def get_queryset(self):
queryset = super().get_queryset()
kw = self.request.query_params.get('kw')
if kw is not None:
queryset = queryset.filter(title__contains=kw)
order = self.request.query_params.get('order')
if order is not None:
fields = ['id', '-id','title','title']
if order in fields:
queryset = queryset.order_by(order)
else:
raise ValidationError({'errror': 'order field must be in ' + ', '.join(fields)})
return queryset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment