将 Django Rest Framework 与序列化程序一起使用时出现 AttributeError
AttributeError while using Django Rest Framework with serializers
我正在学习位于 here that uses Django Rest Framework 的教程,但我不断收到有关某个字段的奇怪错误。
我的 models.py
中有以下模型
from django.db import models
class Task(models.Model):
completed = models.BooleanField(default=False)
title = models.CharField(max_length=100)
description = models.TextField()
然后我的序列化程序在 serializers.py
from rest_framework import serializers
from task.models import Task
class TaskSerializer(serializers.ModelSerializer):
class Meta:
model = Task
fields = ('title', 'description', 'completed')
和我的views.py
如下:
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from task.models import Task
from api.serializers import TaskSerializer
@api_view(['GET', 'POST'])
def task_list(request):
"""
List all tasks, or create a new task
"""
if request.method == 'GET':
tasks = Task.objects.all()
serializer = TaskSerializer(tasks)
return Response(serializer.data)
elif request.method == 'POST':
serializer = TaskSerializer(data=request.DATA)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(
serializer.errors, status=status.HTTP_400_BAD_REQUEST
)
我的 urls.py 有这一行:
url(r'^tasks/$', 'task_list', name='task_list'),
当我尝试访问 curl http://localhost:9000/api/tasks/
时,我不断收到以下错误,我不知道该怎么办:
AttributeError at /api/tasks/
Got AttributeError when attempting to get a value for field `title` on serializer `TaskSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `QuerySet` instance.
Original exception text was: 'QuerySet' object has no attribute 'title'.
我错过了什么?
这里的问题是您试图将条目的查询集(列表)转换为单个条目。解决方案就是沿着这些思路。
from rest_framework import serializers
class TaskListSerializer(serializers.ListSerializer):
child = TaskSerializer()
allow_null = True
many = True
然后
if request.method == 'GET':
tasks = Task.objects.all()
serializer = TaskListSerializer(tasks)
return Response(serializer.data)
从查询集创建序列化程序时简单指定 many=True
,TaskSerializer(tasks)
仅适用于 Task
:
的一个实例
tasks = Task.objects.all()
serializer = TaskSerializer(tasks, many=True)
我正在学习位于 here that uses Django Rest Framework 的教程,但我不断收到有关某个字段的奇怪错误。
我的 models.py
from django.db import models
class Task(models.Model):
completed = models.BooleanField(default=False)
title = models.CharField(max_length=100)
description = models.TextField()
然后我的序列化程序在 serializers.py
from rest_framework import serializers
from task.models import Task
class TaskSerializer(serializers.ModelSerializer):
class Meta:
model = Task
fields = ('title', 'description', 'completed')
和我的views.py
如下:
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from task.models import Task
from api.serializers import TaskSerializer
@api_view(['GET', 'POST'])
def task_list(request):
"""
List all tasks, or create a new task
"""
if request.method == 'GET':
tasks = Task.objects.all()
serializer = TaskSerializer(tasks)
return Response(serializer.data)
elif request.method == 'POST':
serializer = TaskSerializer(data=request.DATA)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(
serializer.errors, status=status.HTTP_400_BAD_REQUEST
)
我的 urls.py 有这一行:
url(r'^tasks/$', 'task_list', name='task_list'),
当我尝试访问 curl http://localhost:9000/api/tasks/
时,我不断收到以下错误,我不知道该怎么办:
AttributeError at /api/tasks/
Got AttributeError when attempting to get a value for field `title` on serializer `TaskSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `QuerySet` instance.
Original exception text was: 'QuerySet' object has no attribute 'title'.
我错过了什么?
这里的问题是您试图将条目的查询集(列表)转换为单个条目。解决方案就是沿着这些思路。
from rest_framework import serializers
class TaskListSerializer(serializers.ListSerializer):
child = TaskSerializer()
allow_null = True
many = True
然后
if request.method == 'GET':
tasks = Task.objects.all()
serializer = TaskListSerializer(tasks)
return Response(serializer.data)
从查询集创建序列化程序时简单指定 many=True
,TaskSerializer(tasks)
仅适用于 Task
:
tasks = Task.objects.all()
serializer = TaskSerializer(tasks, many=True)