如何在Django Rest Fram中显示嵌套对象的属性

2024-04-24 21:29:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Django和Django Rest框架为一个作战系统构建一个API。在我的代码中,我有两个模型:父模型Battle和子模型RoundRound有一些@property字段(start_time, end_time, score),这些字段是基于不同的值计算的。当我直接访问Round路由时,我得到了所需的输出:

http://127.0.0.1:8001/battle/rounds/1/

{
    "id": 1,
    "battle": "http://127.0.0.1:8001/battle/battles/1/",
    "index": 0,
    "contender_entry": null,
    "opponent_entry": null,
    "start_time": "2019-12-11T17:38:00Z",
    "end_time": "2019-12-11T17:39:40Z",
    "score": [
        0,
        0
    ]
}

但是,当我访问Battle路由时,会返回嵌套的Round,但只返回数据库字段,而不返回属性:

http://127.0.0.1:8001/battle/battles/1/

{
    "url": "http://127.0.0.1:8001/battle/battles/1/",
    "id": 1,
    "status": "live",
    "start_time": "2019-12-11T17:38:00Z",
    "round_length": "00:01:40",
    ...
    "rounds": [
        {
            "url": "http://127.0.0.1:8001/battle/rounds/1/",
            "beat": null,
            "index": 0,
            "battle": "http://127.0.0.1:8001/battle/battles/1/",
            "contender_entry": null,
            "opponent_entry": null
        },
        {
            "url": "http://127.0.0.1:8001/battle/rounds/2/",
            "beat": null,
            "index": 1,
            "battle": "http://127.0.0.1:8001/battle/battles/1/",
            "contender_entry": null,
            "opponent_entry": null
        },
        {
            "url": "http://127.0.0.1:8001/battle/rounds/3/",
            "beat": null,
            "index": 2,
            "battle": "http://127.0.0.1:8001/battle/battles/1/",
            "contender_entry": null,
            "opponent_entry": null
        }
    ],
    "current_round": null
}

我希望属性显示在Battle中嵌套的Round对象中。但我没能让它工作。你知道吗

这些是我的模型:

class Round(models.Model):
    battle = models.ForeignKey(Battle, on_delete=models.CASCADE, related_name="rounds")
    index = models.IntegerField()
    contender_entry = models.OneToOneField(Entry, on_delete=models.DO_NOTHING, related_name="round_contender",
                                           null=True)
    opponent_entry = models.OneToOneField(Entry, on_delete=models.DO_NOTHING, related_name="round_opponent", null=True)

    @property
    def start_time(self):
        return self.battle.start_time + (self.index * self.battle.round_length)

    @property
    def end_time(self):
        return self.start_time + self.battle.round_length

    @property
    def score(self):
        opponent_votes = self.votes.filter(favors="opponent").count()
        contender_votes = self.votes.filter(favors="contender").count()
        draw_votes = self.votes.filter(favors="draw").count()
        return (opponent_votes + draw_votes, contender_votes + draw_votes)

class Battle(models.Model):
    status = models.CharField(max_length=32, choices=BATTLE_STATUS_CHOICES, default="awaiting_approval")
    contender = models.ForeignKey(User, on_delete=models.CASCADE, related_name="contender_battles")
    opponent = models.ForeignKey(User, on_delete=models.CASCADE, related_name="opponent_battles")
    start_time = models.DateTimeField(default=timezone.now)
    round_length = models.DurationField(default=timedelta(days=3))

以及序列化程序:

class RoundSerializer(serializers.HyperlinkedModelSerializer):
    id = serializers.IntegerField(source="pk", read_only=True)

    class Meta:
        model = Round
        fields = ["id", "battle", "index", "contender_entry", "opponent_entry", "start_time", "end_time", "score"]
        read_only_fields = ["id", "battle", "index", "start_time", "end_time", "score"]

class BattleSerializer(serializers.HyperlinkedModelSerializer):
    id = serializers.IntegerField(source='pk', read_only=True)
    current_round = RoundSerializer(read_only=True)

    class Meta:
        model = Battle
        fields = ["url", "id", "status", "start_time", "round_length",
                  "opponent", "contender", "rounds", "current_round"]
        read_only_fields = ["contender", "rounds", "status"]


class BattleReadSerializer(BattleSerializer):
    contender = UserSerializer(read_only=True)
    opponent = UserSerializer(read_only=True)

    class Meta:
        model = Battle
        fields = ["url", "id", "status", "start_time", "round_length",
                  "opponent", "contender", "rounds", "current_round"]
        read_only_fields = ["contender", "rounds"]
        depth = 1

请注意,我有两个Battle序列化程序:BattleSerializer用于POST和PUT以及users超链接,而不是嵌套字段。BattleReadSerializer用于获取并嵌套输出。BattleReadSerializer是上面示例中使用的。你知道吗

我已经尝试显式地将字段添加到RoundSerializer,如下所示:

class RoundSerializer(serializers.HyperlinkedModelSerializer):
    id = serializers.IntegerField(source="pk", read_only=True)
    start_time = serializers.DateTimeField(read_only=True)
    score = serializers.ListField(read_only=True)

但这并没有改变什么。有什么方法可以让属性字段显示出来(除了让它们成为db字段并经常重新计算它们之外)?你知道吗


Tags: selftruehttponlyreadtimemodelsstart
1条回答
网友
1楼 · 发布于 2024-04-24 21:29:36

尝试将RoundSerializer序列化程序显式添加到BattleReadSerializer序列化程序,如下所示

class BattleReadSerializer(BattleSerializer):
    contender = UserSerializer(read_only=True)
    opponent = UserSerializer(read_only=True)
    rounds = RoundSerializer(read_only=True, many=True)

    class Meta:
        model = Battle
        fields = ["url", "id", "status", "start_time", "round_length",
                  "opponent", "contender", "rounds", "current_round"]
        read_only_fields = ["contender", "rounds"]
        depth = 1  # remove this

相关问题 更多 >