gpt4 book ai didi

python - 嵌套序列化器中的 Django Rest Framework 关系查询

转载 作者:行者123 更新时间:2023-12-01 06:33:15 31 4
gpt4 key购买 nike

我在 Django REST Framework 中实现嵌套序列化器时遇到困难。
我正在构建一个在线记分板,目前我有三个模型,我正在尝试将其序列化为单个响应。

models.py

class Player(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)

def __str__(self):
return self.first_name


class Event(models.Model):

user = models.ManyToManyField("Player")
name = models.CharField(max_length=50)
desc = models.CharField(max_length=225)

def __str__(self):
return self.name

class LeadBoard(models.Model):
"""model for LeadBoard"""

player = models.ForeignKey(Player, on_delete=models.CASCADE, related_name='leadboard_player', null=True, blank=True)
event = models.ForeignKey(Event, on_delete=models.CASCADE, related_name='leadboard_event', null=True, blank=True)
score = models.IntegerField()

def __str__(self):
return self.score

事件模型代表某种体育赛事,每个事件可以有多个玩家(用户在事件模型中归档),LeadBoard 模型存储每个事件中玩家的得分。

序列化器.py

class PlayerSerializer(serializers.ModelSerializer): 

class Meta:
model = Player
fields = ('id','first_name', 'last_name', 'bio')


class EventSerializer(serializers.ModelSerializer):

user = PlayerSerializer(read_only=True, many=True)
class Meta:
model = Event
fields = ('id', 'name', 'user', 'desc')


class LeadBoardSerializer(serializers.ModelSerializer):

event = EventSerializer(read_only=True)

class Meta:
model = LeadBoard
fields = ('id', 'event', 'score')

我添加了两个玩家

         [
{
"id": 1,
"first_name": "Jhon",
"last_name": "Doe"
},
{
"id": 3,
"first_name": "Anna",
"last_name": "Doe"
}
]

以及他们在 LeadBoard 模型中的得分

[
{
"id": 1,
"player": 1,
"event": 1,
"score": 20
},
{
"id": 2,
"player": 3,
"event": 1,
"score": 90
}
]

这是LeadBoard的回应,

[
{
"id": 1,
"event": {
"id": 1,
"name": "event2020",
"user": [
{
"id": 1,
"first_name": "Jhon",
"last_name": "Doe"
},
{
"id": 3,
"first_name": "Anna",
"last_name": "Doe"
}
],
"desc": "event description"
},
"score": 20
},
{
"id": 2,
"event": {
"id": 1,
"name": "event2020",
"user": [
{
"id": 1,
"first_name": "Jhon",
"last_name": "Doe"
},
{
"id": 3,
"first_name": "Anna",
"last_name": "Doe"
}
],
"desc": "event description"
},
"score": 90
}
]

但是我期望得到的是这样的响应,它会正确返回事件的玩家(用户)及其分数。

[
{
"id": 1,
"event": {
"id": 1,
"name": "event2020",
"user": [
{
"id": 1,
"first_name": "Jhon",
"last_name": "Doe",
"score": 20
},
{
"id": 3,
"first_name": "Anna",
"last_name": "Doe",
"score": 90
}
],
"desc": "event description"
}
}
]

我在这里缺少什么?

我是 Django 和 Django Rest Framework 的新手。

最佳答案

您需要按事件而不是排行榜做出响应。

class PlayerSerializer(serializers.ModelSerializer): 

class Meta:
model = Player
fields = ('id','first_name', 'last_name', 'bio')

class LeadBoardSerializer(serializers.ModelSerializer):

player = PlayerSerializer(read_only=True)

class Meta:
model = LeadBoard
fields = ('id', 'player', 'score')

class EventSerializer(serializers.ModelSerializer):

leadboard_event = LeadBoardSerializer(read_only=True, many=True)

class Meta:
model = Event
fields = ('id', 'name', 'desc', 'leadboard_event')

现在使用 View 来获取事件列表

更新 1如果你想在玩家中获得分数。

class PlayerSerializer(serializers.Serializer):  
player_id = serializers.IntegerField()
first_name = serializers.CharField(max_length=256)
last_name = serializers.CharField(max_length=256)
score = serializers.IntegerField()

class LeadBoardSerializer(serializers.ModelSerializer):

player = serializers.SerializerMethodField()

class Meta:
model = LeadBoard
fields = ('id', 'player')

def get_player(self,obj):
player_dict = {'player_id': obj.player.id, 'first_name': obj.player.first_name, 'last_name': obj.player.last_name, 'score': obj.score}
result = PlayerSerializer(data=player_dict)
return result

class EventSerializer(serializers.ModelSerializer):

leadboard_event = LeadBoardSerializer(read_only=True, many=True)

class Meta:
model = Event
fields = ('id', 'name', 'desc', 'leadboard_event')

尝试一下。

关于python - 嵌套序列化器中的 Django Rest Framework 关系查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59793874/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com