运行DRF序列化程序的结果为空数据

时间:2019-12-09 05:02:29

标签: django django-rest-framework

我正在尝试为现有项目创建API。我为此创建了一个序列化器并创建了一个视图,尽管通过打开可浏览的API,我得到了空括号。 这是视图:

class GroupAPIView(mixins.CreateModelMixin, generics.ListAPIView):
    serializer_class = GroupSerializer
    queryset = Group.objects.all()

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)


class GroupMemberAPIView(mixins.CreateModelMixin, generics.ListAPIView):
    serializer_class = GroupMemberSerializer
    queryset = GroupMember.objects.all()

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

序列化器是:

class GroupSerializer(serializers.Serializer):
    class Meta:
        model = Group
        fields = "__all__"
    # fields = ['name']
class GroupMemberSerializer(serializers.Serializer):
    class Meta:
        model = GroupMember
        fields = "__all__"

这就是我看到的结果:

HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

[
{},
{},
{},
{}

]

为什么这些模型中显示某些数据,但显示的却是空数据。请有人帮忙。

2 个答案:

答案 0 :(得分:2)

使用serializers.ModelSerializer代替serializers.Serializer

答案 1 :(得分:2)

您应该继承[ {2, 5, 7, 12, 19, 25, 28, 29}, {8, 12, 14, 15, 16, 18, 19, 21, 24, 28}, {2, 7, 15, 17, 21, 24, 26, 27, 30} ] 而不是ModelSerializer

serializer