This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SingleArticleView(RetrieveUpdateAPIView): | |
| queryset = Article.objects.all() | |
| serializer_class = ArticleSerializer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SingleArticleView(RetrieveAPIView): | |
| queryset = Article.objects.all() | |
| serializer_class = ArticleSerializer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ArticleView(ListCreateAPIView): | |
| queryset = Article.objects.all() | |
| serializer_class = ArticleSerializer | |
| def perform_create(self, serializer): | |
| author = get_object_or_404(Author, id=self.request.data.get('author_id')) | |
| return serializer.save(author=author) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ArticleView(CreateAPIView, ListAPIView): | |
| queryset = Article.objects.all() | |
| serializer_class = ArticleSerializer | |
| def perform_create(self, serializer): | |
| author = get_object_or_404(Author, id=self.request.data.get('author_id')) | |
| return serializer.save(author=author) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ArticleView(ListModelMixin, CreateModelMixin, GenericAPIView): | |
| queryset = Article.objects.all() | |
| serializer_class = ArticleSerializer | |
| def perform_create(self, serializer): | |
| author = get_object_or_404(Author, id=self.request.data.get('author_id')) | |
| return serializer.save(author=author) | |
| def get(self, request, *args, **kwargs): | |
| return self.list(request, *args, *kwargs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from rest_framework.generics import GenericAPIView | |
| from rest_framework.mixins import ListModelMixin | |
| from .models import Article | |
| from .serializers import ArticleSerializer | |
| class ArticleView(ListModelMixin, GenericAPIView): | |
| queryset = Article.objects.all() | |
| serializer_class = ArticleSerializer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ArticleSerializer(serializers.ModelSerializer): | |
| class Meta: | |
| model = Article | |
| fields = ('id', 'title', 'description', 'body', 'author_id') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ArticleView(ListModelMixin, GenericAPIView): | |
| queryset = Article.objects.all() | |
| serializer_class = ArticleSerializer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def delete(self, request, pk): | |
| # Get object with this pk | |
| article = get_object_or_404(Article.objects.all(), pk=pk) | |
| article.delete() | |
| return Response({"message": "Article with id `{}` has been deleted.".format(pk)},status=204) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def put(self, request, pk): | |
| saved_article = get_object_or_404(Article.objects.all(), pk=pk) | |
| data = request.data.get('article') | |
| serializer = ArticleSerializer(instance=saved_article, data=data, partial=True | |
| if serializer.is_valid(raise_exception=True): | |
| article_saved = serializer.save() | |
| return Response({"success": "Article '{}' updated successfully".format(article_saved.title)}) |