我有一个带有字段的类评论:分数,作者,Id,pub_date 这是我在序列化程序中的代码
class CommentSerializer(serializers.ModelSerializer):
author = serializers.SlugRelatedField(read_only=True,slug_field='username')
class Meta:
fields = ('id','text','author','score', 'pub_date')
model = Comment
意见:
class CommentViewSet(ModelViewSet):
queryset = Comment.objects.all()
serializer_class = CommentSerializer
permission_classes = [MyPermissionClass]
pagination_class = MyLimitOffsetPagination
def get_queryset(self):
post = get_object_or_404(Post, pk=self.kwargs.get('post_id'))
return post.comment_set
def perform_create(self, serializer):
post = get_object_or_404(Post, pk=self.kwargs.get('post_id'))
a=serializer(author=self.request.user, post=post)
if a.score<50:
a.save()
else:
Response({'number':'must be less than 50'}, status=status.HTTP_400_BAD_REQUEST)
但它不起作用,它不仅没有将score的值限制为50,而且还破坏了整个代码