gpt4 book ai didi

python - Django Rest Framework嵌套序列化器对象为空

转载 作者:行者123 更新时间:2023-12-04 10:51:17 25 4
gpt4 key购买 nike

我有一个使用 Django Rest Framework 的两个模型设置。一个模型是嵌套到另一个对象的序列化器。一种型号是 播放器 另一个型号是团队数据 .我希望 TeamData 嵌套到 Player 中。

# models.py
from django.db import models


class TeamData(models.Model):
urlName = models.CharField(max_length=50)
city = models.CharField(max_length=50)
nickname = models.CharField(max_length=50)
tricode = models.CharField(max_length=3)

def __str__(self):
return self.city


class Player(models.Model):
firstName = models.CharField(max_length=100)
lastName = models.CharField(max_length=100)
jersey = models.CharField(max_length=10)
pos = models.CharField(max_length=10)
posExpanded = models.CharField(max_length=10)
heightFeet = models.CharField(max_length=2)
heightInches = models.CharField(max_length=2)
weightPounds = models.CharField(max_length=5)
personId = models.CharField(max_length=50)
teamData = models.ForeignKey(
TeamData, on_delete=models.CASCADE, null=True, blank=True)
isAllStar = models.BooleanField()
orderChar = models.CharField(max_length=1)
playerUrl = models.CharField(max_length=50)
displayName = models.CharField(max_length=100)



#serializer.py
from rest_framework import serializers
from .models import *


class TeamDataSerializer(serializers.ModelSerializer):
class Meta:
model = TeamData
fields = ('id', 'urlName', 'city', 'nickname', 'tricode')


class PlayerSerializer(serializers.ModelSerializer):

teamData = TeamDataSerializer('teamData')

class Meta:
model = Player
fields = ('id', 'firstName', 'lastName', 'jersey', 'pos',
'posExpanded', 'heightInches', 'weightPounds', 'personId',
'teamData', 'isAllStar', 'orderChar', 'playerUrl', 'displayName')

def create(self, validated_data):
teamData = validated_data.pop('teamData')
player = Player.objects.create(**validated_data)
return player


#views.py
from rest_framework import viewsets
from .models import Player, TeamData
from rest_framework import viewsets, permissions
from .serializers import *
class PlayerViewSet(viewsets.ModelViewSet):
queryset = Player.objects.all()
permission_classes = [
permissions.AllowAny
]
serializer_class = PlayerSerializer

当我做一个 POST 请求时
{
"firstName": "Ste",
"lastName": "Adams",
"jersey": "12",
"pos": "C",
"posExpanded": "Center",
"heightFeet": "6",
"heightInches": "11",
"weightPounds": "265",
"personId": "203500",
"teamData": {
"id": "1",
"urlName": "thunder",
"city": "Oklahoma City",
"nickname": "Thunder",
"tricode": "OKC"
},
"isAllStar": false,
"orderChar": "A",
"playerUrl": "/players/steven/adams/203500",
"displayName": "Adams, Steven"
}

我回来了
{
"id": 6,
"firstName": "Ste",
"lastName": "Adams",
"jersey": "12",
"pos": "C",
"posExpanded": "Center",
"heightInches": "11",
"weightPounds": "265",
"personId": "203500",
"teamData": null,
"isAllStar": false,
"orderChar": "A",
"playerUrl": "/players/steven/adams/203500",
"displayName": "Adams, Steven"
}

知道为什么 teamData 为空吗?我怀疑它可能与
View ,但我不确定问题是什么。

最佳答案

您正在传递一个 字符串 PlayerSerializer 没原因。它应该是,

class PlayerSerializer(serializers.ModelSerializer):
teamData = TeamDataSerializer() # without passing anything to the serializer

# other code

另外,您不是在创建 TeamData 您的 中的实例create() 序列化器的方法。
#final code sample
class PlayerSerializer(serializers.ModelSerializer):
teamData = TeamDataSerializer()

class Meta:
model = Player
fields = ('id', 'firstName', 'lastName', 'jersey', 'pos',
'posExpanded', 'heightInches', 'weightPounds', 'personId',
'teamData', 'isAllStar', 'orderChar', 'playerUrl', 'displayName')

def create(self, validated_data):
validated_data['teamData'] = TeamData.objects.create(**validated_data.get('teamData', {}))
player = Player.objects.create(**validated_data)
return player

关于python - Django Rest Framework嵌套序列化器对象为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59459926/

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