0
我是django rest框架的新手。
我的模型的相关部分看起来像这样(简化):在Django REST框架中制作自定义布尔字段
class Action(models.Model):
type = models.CharField()
bonus = models.IntegerField()
class User(models.Model):
auth = models.OneToOneField(AuthUser)
display_name = models.CharField()
class Comment(models.Model):
contributor = models.ForeignKey(User)
content = models.TextField()
class Activity(models.Model):
user = models.ForeignKey(User)
target_comment = models.ForeignKey(Comment)
action = models.ForeignKey(Action)
我希望我的API端点之一,能够回到这样的事情:
{
...
"comments":
[
{
"id": 1,
...
"curr_user_upvoted": true
},
...
]
}
我知道查询我需要执行以获得“curr_user_upvoted”的值,但我不知道如何使它成为API表示的一部分。
我知道如何创建自定义关系字段,但它没有帮助,因为“curr_user_upvoted”既不是字段也不是关系。
有什么想法?