Вот как я создаю свою статью
`class CreateArticleView (ListCreateAPIView):" "" Класс обрабатывает создание статей "" "
permission_classes = (IsAuthenticatedOrReadOnly,)
serializer_class = ArticleSerializer
renderer_classes = (ArticleJSONRenderer,)
queryset = Article.objects.all()
def list(self, request, *args, **kwargs):
queryset = Article.objects.all()
serializer = self.serializer_class(queryset, many=True)
return Response(serializer.data)
def post(self, request, *args, **kwargs):
article = request.data.get('article', {})
if self.request.user.is_verified is False:
message = error_messages['email_verification']
return Response(message, status=status.HTTP_401_UNAUTHORIZED)
context = {"request": request}
serializer = self.serializer_class(data=article, context=context)
serializer.is_valid(raise_exception=True)
serializer.save(author=request.user)
return Response(serializer.data, status=status.HTTP_201_CREATED)`
`
Вот как я могу просмотреть одну статью, поэтому я хотел бы получить одну статью, выделить несколько частей статьи и прокомментировать их
class GetUpdateDeleteArticle(RetrieveUpdateDestroyAPIView):
permission_classes = (IsAuthenticated,)
renderer_classes = (ArticleJSONRenderer,)
queryset = Article.objects.all()
serializer_class = ArticleSerializer
lookup_field = 'slug'
@staticmethod
def validate_author(request, article):
if request.user.pk != article.author_id:
message = error_messages['unauthorised']
return Response(message, status.HTTP_403_FORBIDDEN)
def get(self, request, *args, **kwargs):
"""
:param request: user requests to get an article
:param kwargs: slug field is passed in the url
:return: data and response if article exists
"""
try:
article = Article.objects.get(slug=kwargs['slug'])
except Article.DoesNotExist:
message = error_messages['article_404']
return Response(message, status=status.HTTP_404_NOT_FOUND)
serializer = ArticleSerializer(
instance=article, context={'request': request})
return Response(serializer.data, status=status.HTTP_200_OK)