demo-hackernews-with-django.../app/api/views.py
Andros Fenollosa 4d6f73ac63 Finish version
2021-06-25 14:25:14 +02:00

30 lines
911 B
Python

from app.api.models import News
from rest_framework import viewsets, mixins
from app.api.serializers import NewsSerializer
from rest_framework.response import Response
import json
class NewsViewSet( mixins.UpdateModelMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet):
"""
API endpoint that allows News to be viewed or edited.
"""
serializer_class = NewsSerializer
queryset = News.objects.all()
basename = "news"
def get(self, request):
serializer = NewsSerializer(self.queryset, many=True)
return Response(serializer.data)
def put(self, request):
# Update
my_news = News.objects.get(pk=request.data['id'])
my_news.votes = my_news.votes + 1
my_news.save()
# Devolvemos la noticia modificada
serializer = NewsSerializer(my_news)
return Response(serializer.data)