Django rest serializer.data 是一个空的 OrderedDict()
Django rest serializer.data is an empty OrderedDict()
这是我的模型:
from django.db import models
from django.contrib.auth.models import User
from datetime import datetime
class Staff(models.Model):
employer = models.ForeignKey("shops.Shop")
user = models.ForeignKey(User)
def __unicode__(self):
return self.user.username
这是我的序列化程序:
from rest_framework import serializers
from staff.models import Staff
class StaffSerializer(serializers.Serializer):
class Meta:
model = Staff
fields = ("id", "employer", "user")
这是我的观点:
from staff.models import Staff
from staff.serializers import StaffSerializer
from rest_framework import generics
from rest_framework.response import Response
class StaffList(generics.ListCreateAPIView):
queryset = Staff.objects.all()
serializer_class = StaffSerializer
def list(self, request):
queryset = self.get_queryset()
serializer = StaffSerializer(queryset, many=True)
print queryset
print serializer
print serializer.data
return Response(serializer.data)
在网络上查看此页面时可浏览 API 这是我看到的:
HTTP 200 OK
Content-Type: application/json
Vary: Accept
Allow: GET, POST, HEAD, OPTIONS
[
{}
]
打印serializer.data和queryset的结果是这样的:
{<Staff: Alex>}
StaffSerializer([<Staff: Alex>], many=True):
[OrderedDict()]
我的序列化器有问题还是这又是另外一个问题?
如果您使用模型和查询集,则需要使用 serializers.ModelSerializer 而不是 serializers.Serializer。
如果您正在使用 serializers.Serializer,您需要在其中手动定义字段。像这样:
class StaffSerializer(serializers.Serializer):
id = serializers.IntegerField()
content = serializers.CharField(max_length=200)
等
您不能像 ModelSerializer 那样在元数据中引用它们。可以找到有关 ModelSerializer 的文档 here
这是我的模型:
from django.db import models
from django.contrib.auth.models import User
from datetime import datetime
class Staff(models.Model):
employer = models.ForeignKey("shops.Shop")
user = models.ForeignKey(User)
def __unicode__(self):
return self.user.username
这是我的序列化程序:
from rest_framework import serializers
from staff.models import Staff
class StaffSerializer(serializers.Serializer):
class Meta:
model = Staff
fields = ("id", "employer", "user")
这是我的观点:
from staff.models import Staff
from staff.serializers import StaffSerializer
from rest_framework import generics
from rest_framework.response import Response
class StaffList(generics.ListCreateAPIView):
queryset = Staff.objects.all()
serializer_class = StaffSerializer
def list(self, request):
queryset = self.get_queryset()
serializer = StaffSerializer(queryset, many=True)
print queryset
print serializer
print serializer.data
return Response(serializer.data)
在网络上查看此页面时可浏览 API 这是我看到的:
HTTP 200 OK
Content-Type: application/json
Vary: Accept
Allow: GET, POST, HEAD, OPTIONS
[
{}
]
打印serializer.data和queryset的结果是这样的:
{<Staff: Alex>}
StaffSerializer([<Staff: Alex>], many=True):
[OrderedDict()]
我的序列化器有问题还是这又是另外一个问题?
如果您使用模型和查询集,则需要使用 serializers.ModelSerializer 而不是 serializers.Serializer。
如果您正在使用 serializers.Serializer,您需要在其中手动定义字段。像这样:
class StaffSerializer(serializers.Serializer):
id = serializers.IntegerField()
content = serializers.CharField(max_length=200)
等 您不能像 ModelSerializer 那样在元数据中引用它们。可以找到有关 ModelSerializer 的文档 here