【发布时间】:2020-04-06 16:09:10
【问题描述】:
所以我正在尝试创建一个序列化程序,它会在我的项目中返回一堆“发布”模型的详细信息,但我也想获取嵌套对象的详细信息,我将在下面显示其输出:
后模型:
class Post(models.Model):
# Base post attributes
user = models.ForeignKey(UserOfApp, related_name="posts", on_delete=models.CASCADE)
...
# Repost post attributes
original_repost = models.ForeignKey(
to="self",
related_name="reposted_post",
null=True,
blank=True,
on_delete=models.CASCADE,
)
# Quote post attributes
is_quote_post = models.BooleanField(default=False)
quoted_post = models.ForeignKey(
to="self", null=True, blank=True, on_delete=models.CASCADE
)
...
我的序列化器,使用“深度”:
class HomeSerializer(serializers.ModelSerializer):
id_str = serializers.ReadOnlyField()
...
quoted_post_id_str = serializers.ReadOnlyField()
class Meta:
model = Post
depth = 2
fields = "__all__"
这会产生类似的输出,问题是它也会从我的用户模型返回所有敏感数据,例如“密码”等:
{
"id": 16,
...
"is_quote_post": false,
"user": {
"id": 1,
"password": "pbkdf2_sha256$180000$ni6drJvLjUU2$xoMt5tpJmz8TlmORfB/eTkYlGGpUGfriGz8rO7kVB8E=",
"last_login": "2020-03-21T06:20:48Z",
"is_superuser": true,
"username": "manas",
"first_name": "Manas",
"last_name": "Acharekar",
"email": "manas.acharekar98@gmail.com",
"is_staff": true,
"is_active": true,
"date_joined": "2020-03-01T18:42:11Z",
"dob": "1998-11-11",
"gender": "M",
"bio": "i own this place",
"location": "",
"website": "",
"verified": true,
"followers": [],
"friends": [],
"groups": [],
"user_permissions": []
},
"original_repost": {
"id": 1,
...
"user": {
"id": 1,
"password": "pbkdf2_sha256$180000$ni6drJvLjUU2$xoMt5tpJmz8TlmORfB/eTkYlGGpUGfriGz8rO7kVB8E=",
"last_login": "2020-03-21T06:20:48Z",
"is_superuser": true,
"username": "manas",
"first_name": "Manas",
"last_name": "Acharekar",
"email": "manas.acharekar98@gmail.com",
"is_staff": true,
"is_active": true,
"date_joined": "2020-03-01T18:42:11Z",
"dob": "1998-11-11",
"gender": "M",
"bio": "i own this place",
"location": "",
"website": "",
"verified": true,
"followers": [],
"friends": [],
"groups": [],
"user_permissions": []
},
"original_repost": null,
"quoted_post": null
},
"quoted_post": null
}
我的序列化器,不使用深度:
class HomeSerializer(serializers.ModelSerializer):
user = UserSerializer()
id_str = serializers.ReadOnlyField()
...
quoted_post_id_str = serializers.ReadOnlyField()
quoted_post = PostSerializer()
original_repost = PostSerializer()
class Meta:
model = Post
fields = "__all__"
这会正确输出帖子,但我无法获得完整的嵌套对象。在下面提到的返回 JSON 中,我想访问 quoted_post.user 或 original_repost.user,但只返回一个外键:
{
"id": 16,
"user": {
"id": 1,
"followers_count": 0,
"friends_count": 0,
"username": "manas",
"first_name": "Manas",
"last_name": "Acharekar",
"email": "manas.acharekar98@gmail.com",
"date_joined": "2020-03-01T18:42:11Z",
"dob": "1998-11-11",
"gender": "M",
"bio": "i own this place",
"location": "",
"website": "",
"verified": true,
"followers": [],
"friends": []
},
"id_str": "16",
...
"quoted_post": null,
"original_repost": {
"id": 1,
...
"is_quote_post": false,
"user": 1,
"original_repost": null,
"quoted_post": null
},
...
"is_quote_post": false
}
我该如何解决这个问题?
【问题讨论】:
-
使用嵌套序列化程序
-
@ArakkalAbu 我该怎么做?可以发一些相关的链接吗?
标签: python json django rest django-rest-framework