首页 > 解决方案 > 通过 UUID Django 过滤时的验证错误

问题描述

我正在尝试返回作为关系作者的某个用户的朋友的所有朋友。

但是,我不断收到此错误:

/author/posts 处的 ValidationError [““[UUID('8c02a503-7784-42f0-a367-1876bbfad6ff')]”不是有效的 UUID。”]

class Author(AbstractUser):
    ...
    uuid = models.UUIDField(primary_key=True, default=uuid4, editable=False, unique=True)
    ...

class Post(models.Model):
    ...
    author = models.ForeignKey(Author, on_delete=models.CASCADE)
    ...

class Friend(models.Model):
    class Meta:
        unique_together = (('author','friend'),)
    author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='author')
    friend = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='friend')

这条 foaf 线尤其是错误的来源。我还能怎么做?

friends = Friend.objects.filter(author=userUUID)
foafs = Friend.objects.filter(friend=[friend.friend.uuid for friend in friends])

标签: pythondjangovalidationdjango-modelsdjango-rest-framework

解决方案


推荐阅读