Answer the question
In order to leave comments, you need to log in
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('Текст')
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)
class CommentSerializer(serializers.ModelSerializer):
class Meta:
model = Comment
fields = ['author', 'quiz', 'body']
Answer the question
In order to leave comments, you need to log in
Removed author and quiz from the serializer. Everything
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question