Django restful 框架 "The serializer field might be named incorrectly"

Django restful framework "The serializer field might be named incorrectly"

你好亲爱的堆栈溢出者, 我用 restful 框架建立了一个 Django 项目,我使用 JSON 响应以结构化的方式从服务器检索数据。

models.py

class Container(models.Model):     
    created = models.DateTimeField(auto_now_add=True)     
    title = models.CharField(max_length=100, blank=False, default='content')     
    content = models.TextField()      

        class Meta:         
            ordering = ['created']

有问题的代码:

views.py

class ContentView(RetrieveAPIView):
    queryset = Container.objects.all()
    serializer_class = ContainerSerializer
    def get_object(self):
        queryset = self.get_queryset()
        serializer = ContainerSerializer(queryset,many=True)
        return JsonResponse(serializer.data , safe=False)

执行时出现如下错误:

AttributeError: Got AttributeError when attempting to get a value for field `title` on serializer `ContainerSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `JsonResponse` instance.
Original exception text was: 'JsonResponse' object has no attribute 'title'.
[21/May/2021 17:27:13] "GET /content/ HTTP/1.1" 500 18789

为了尽可能清楚,我将附上序列化程序的代码:

serializers.py

class ContainerSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    title = serializers.CharField(required=True, allow_blank=False, max_length=100)
    content = serializers.CharField()

    def create(self, validated_data):
        return Container.objects.create(**validated_data)

    def update(self, instance, validated_data):
        instance.title = validated_data.get('title', instance.title)
        instance.content = validated_data.get('content', instance.content)
        instance.save()
        return instance

在切换到 RetrieveAPIView 之前,一切都在测试服务器上运行良好。 切换到这种新的请求方法时出现错误。

任何帮助将不胜感激!

让我们像这样尝试模型序列化程序:记住,您需要更新 serializers.py、views.py 和 urls.py 文件才能使其正常工作:

serialiers.py

from rest_framework import serializers
from . import models

class ContainerSerializer(serializers.ModelSerializer):

    class Meta:
        model = models.Container
        fields = ['id', 'created', 'title', 'content']

views.py

from rest_framework import viewsets
from . import serializers
from . import models

class ContainerViewSet(viewsets.ModelViewSet):

    serializer_class = serializers.ContainerSerializer
    queryset = models.Container.objects.order_by('id')

urls.py

from django.urls import include, path
from rest_framework import routers
from . import views

# rest:
router = routers.DefaultRouter()
router.register('containers', views.ContainerViewSet)

urlpatterns = [
    ...
    path('api/', include(router.urls))
]

现在我们可以完全访问 rest,所以我们可以执行 getputpostpatchdelete 请求等. 对 url <base-url>/api/containersget 请求应该 return 类似于:

[
  {
    'id' : 1,
    'created' : 'YYYY-MM-DD',
    'title' : 'title-1',
    'content' : 'content-1'
  },
  {
    'id' : 2,
    'created' : 'YYYY-MM-DD',
    'title' : 'title-2',
    'content' : 'content-2'
  }
]