M
M
MalikDeveloper20772020-02-27 10:00:58
Django
MalikDeveloper2077, 2020-02-27 10:00:58

Why HTTP 400 when creating a DRF comment?

There is a model:

class Comment(models.Model):
    """Comment for a quiz"""
    quiz = models.ForeignKey(
        Quiz,
        on_delete=models.CASCADE,
        verbose_name='Викторина',
        related_name='comments'
    )
    author = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.PROTECT,
        verbose_name='Автор',
        related_name='comments'
    )
    body = models.TextField('Текст')


I send a request via ajax, all data is transmitted, checked (in data: {} body and csrf token)
Next View
class CommentCreateAPI(APIView):
    """API for create a quiz comment. Get a quiz slug"""
    def post(self, request, slug):
        quiz = get_object_or_404(Quiz, slug=slug)
        serializer = CommentSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save(author=request.user, quiz=quiz)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


And serializer
class CommentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Comment
        fields = ['author', 'quiz', 'body']


All data in request.data comes in, but HTTP 400 crashes to me, what is the problem and how to solve it?/

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MalikDeveloper2077, 2020-02-27
@MalikDeveloper2077

Removed author and quiz from the serializer. Everything

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question