我遇到嵌套序列化程序和Django休息框架的问题。 将serializer_class设置为" UserSerializer"时,来自" NameSerializer"的嵌套数据上课没有出现。 但是,当我将serializer_class设置为" NameSerializer"时,数据确实会出现。
使用UserSerializer时返回API:
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
[
{
"id": 1,
"userName": "admin@admin.com"
},
{
"id": 2,
"userName": "2@admin.com"
},
{
"id": 3,
"userName": "3@admin.com"
},
{
"id": 4,
"userName": "4@admin.com"
}
]
我希望它返回:
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
[
{
"id": 1,
"userName": "admin@admin.com"
"name": {
"familyName": "Joe",
"givenName": "admin",
"formated": "admin Joe"
},
},
{
"id": 2,
"userName": "jane@admin.com"
"name": {
"familyName": "Doe",
"givenName": "Jane",
"formated": "Jane Doe"
},
},
{
"id": 3,
"userName": "Billy@admin.com"
"name": {
"familyName": "Idol",
"givenName": "Billy",
"formated": "Billy Idol"
},
},
{
"id": 4,
"userName": "User@admin.com"
"name": {
"familyName": "B",
"givenName": "User",
"formated": "User B"
},
}
]
views.py
from API.models import Person
from API.serializers import *
from rest_framework import viewsets
class UserViewSet(viewsets.ModelViewSet):
queryset = Person.objects.all()
serializer_class = NameSerializer
models.py
class Person(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
first_name = models.CharField(max_length=254, blank=True)
last_name = models.CharField(max_length=254, blank=True)
serializers.py
class NameSerializer(serializers.ModelSerializer):
familyName = serializers.CharField(source='last_name')
givenName = serializers.CharField(source='first_name')
formatted = serializers.SerializerMethodField()
class Meta:
model = Person
fields = ('familyName', 'givenName', 'formatted',)
def get_formatted(slef, obj):
FullName = obj.first_name + '' + obj.last_name
return FullName
class UserSerializer(serializers.ModelSerializer):
name = NameSerializer(many=True, readonly=True)
userName = serializers.EmailField(source='email')
class Meta:
model = Person
fields = ('id', 'userName', 'name')
感谢任何帮助
答案 0 :(得分:3)
您不需要创建NameSerializer
序列化程序。在UserSerializer
字段中为name
字段创建方法字段,例如:
class UserSerializer(serializers.ModelSerializer):
userName = serializers.EmailField(source='email')
name = serializers.SerializerMethodField()
class Meta:
model = Person
fields = ('id', 'userName', 'name')
def get_name(self, obj):
name = {
'familyName': obj.first_name,
'givenName': obj.last_name,
'formatted': obj.first_name + ' ' + obj.last_name
}
return name
它会给你想要的结果。
答案 1 :(得分:0)
也许尝试使用SerializerMethodField: 像这样:
class UserSerializer(serializers.ModelSerializer):
userName = serializers.EmailField(source='email')
name = serializers.SerializerMethodField()
class Meta:
model = Person
fields = ('id', 'userName', 'name')
def get_name(self, obj):
return {
'familyName': obj.first_name,
'givenName': obj.last_name,
'formatted': obj.first_name + ' ' + obj.last_name
}
答案 2 :(得分:0)
这可能会对您有所帮助:
class UserSerializer(serializers.ModelSerializer):
name = NameSerializer()
userName = serializers.EmailField(source='email')
class Meta:
model = Person
fields = ('id', 'userName', 'name')
如果项目应该是列表,那么只需要传递many=True
。