[“' '不是有效的UUID。”]

问题描述 投票:0回答:1

我正在尝试修补现有的但我遇到了未知的问题。有人知道为什么吗?

Models.py

class Article(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    author = models.ForeignKey(UserProfile,on_delete=models.CASCADE, related_name='articles')
    caption = models.CharField(max_length=250)

Views.py

class ArticleView(CreateAPIView):
  serializer_class = ArticleCreateSerializer
  permission_classes = (IsAuthenticated,)

  def patch(self, request, *args, **kwargs):
        article = get_object_or_404(Article, pk=id)
        serializer = ArticleCreateSerializer(data=request.data, partial=True)
        if serializer.is_valid():
            article = serializer.save()
            return Response(ArticleCreateSerializer(article).data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

错误:

["'<built-in function id>' is not a valid UUID."]
django
1个回答
0
投票

article = get_object_or_404(Article, pk=id)

[id这里是内置的python function

article = get_object_or_404(Article, pk=kwargs.get('id'))

将是正确的。

© www.soinside.com 2019 - 2024. All rights reserved.