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 37 38 39 40
| from articles.models import Article from rest_framework import viewsets, status from rest_framework.response import Response from .serializers import ArticleSerializer from django.shortcuts import get_object_or_404
class ArticleViewSet(viewsets.ModelViewSet): serializer_class = ArticleSerializer queryset = Article.objects.all()
def list(self, request): queryset = Article.objects.all() serializer = self.serializer_class(queryset, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
def retrieve(self, request, pk=None): article = get_object_or_404(self.queryset, pk=pk) serializer = self.serializer_class(article) return Response(serializer.data)
def post(self, request): serializer = self.serializer_class(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def update(self, request, pk=None, *args, **kwargs): article = get_object_or_404(self.queryset, pk=pk) serializer = self.serializer_class(article, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def destroy(self, request, pk=None): article = get_object_or_404(self.queryset, pk=pk) article.delete() return Response(None, status=status.HTTP_204_NO_CONTENT)
|