-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
36 lines (26 loc) · 1.23 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# from django import views
from rest_framework import viewsets
from django_filters.rest_framework import DjangoFilterBackend
from .models import Task, Category
from .serializers import TaskSerializer, CategorySerializer
from rest_framework.permissions import IsAuthenticated
from rest_framework.filters import SearchFilter
class CategoryViewSet(viewsets.ModelViewSet):
serializer_class = CategorySerializer
queryset = Category.objects.all()
permission_classes = [IsAuthenticated]
def get_queryset(self):
return self.queryset.filter(user = self.request.user)
def perform_create(self, serializer):
serializer.save(user = self.request.user)
class TaskViewSet(viewsets.ModelViewSet):
serializer_class = TaskSerializer
queryset = Task.objects.all()
permission_classes = [IsAuthenticated]
filter_backends = [DjangoFilterBackend, SearchFilter]
filterset_fields = ['status', 'due_date', 'category']
search_fields = ['title', 'description']
def get_queryset(self):
return self.queryset.filter(user = self.request.user)
def perform_create(self, serializer):
serializer.save(user = self.request.user)