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」既不是字段也不是關係。
有什麼想法?