我收到了NotimplementError。我正在研究Django的restframework身份验证和权限。我能够通过管理面板输入数据,但是当涉及到序列化器时,我收到了这个错误。
以下是错误报告:
NotImplementedError at /api/tasks/
Field.to_representation() must be implemented for field owner. If you do not need to support write operations you probably want to subclass `ReadOnlyField` instead.
Request Method: GET
Request URL: http://127.0.0.1:8000/api/tasks/
Django Version: 1.9
Exception Type: NotImplementedError
Exception Value:
Field.to_representation() must be implemented for field owner. If you do not need to support write operations you probably want to subclass `ReadOnlyField` instead.
Exception Location: /home/graymatter/test/bookenv/local/lib/python2.7/site-packages/rest_framework/fields.py in to_representation, line 529
Python Executable: /home/graymatter/test/bookenv/bin/python
Python Version: 2.7.6
Python Path:
['/home/graymatter/test/todo',
'/home/graymatter/test/bookenv/lib/python2.7',
'/home/graymatter/test/bookenv/lib/python2.7/plat-x86_64-linux-gnu',
'/home/graymatter/test/bookenv/lib/python2.7/lib-tk',
'/home/graymatter/test/bookenv/lib/python2.7/lib-old',
'/home/graymatter/test/bookenv/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/home/graymatter/test/bookenv/local/lib/python2.7/site-packages']
Server time: Tue, 8 Mar 2016 09:03:24 +0000
这是我的serializers.py
from rest_framework import serializers
from .models import Task
class TaskSerializers(serializers.ModelSerializer):
owner = serializers.Field(source='owner.username')
class Meta:
model = Task
fields = ('title', 'desc', 'completed', 'owner')
views.py:
from django.shortcuts import render
from rest_framework import generics
from rest_framework import mixins
from .models import Task
from .serializers import TaskSerializers
from .permissions import IsOwnerOrReadOnly
# Create your views here.
class TaskMixin(object):
queryset = Task.objects.all()
serializer_class = TaskSerializers
permission_classes = (IsOwnerOrReadOnly,)
def pre_save(self, obj):
obj.owner = self.request.user
class TaskDetail(TaskMixin, generics.RetrieveUpdateAPIView):
pass
class TaskList(TaskMixin, generics.ListCreateAPIView):
pass
下面是models.py
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Task(models.Model):
owner = models.ForeignKey('auth.user', related_name='tasks')
completed = models.BooleanField(default=False, verbose_name='Completed')
title = models.CharField(max_length=200, verbose_name='Title')
desc = models.TextField(verbose_name='Description')
def __unicode__(self):
return self.title
下面的是permissions.py
from rest_framework.permissions import BasePermission, SAFE_METHODS
class IsOwnerOrReadOnly(BasePermission):
def has_object_permission(self, request, view, obj):
if request.method in SAFE_METHODS:
return True
return obj.owner == request.user
我是Django和rest框架的初学者。我正在关注https://developers.facebook.com/policy/教程。现在我被困在这里。请帮忙。
感谢!!!!
答案 0 :(得分:3)
也许教程已经过时了。与错误消息一样,您应该将序列化程序中的owner
字段更改为ReadOnlyField
(docs)
答案 1 :(得分:2)
确实,改变了
owner = serializers.Field(source='owner.username')
到
owner = serializers.ReadOnlyField(source='owner.username')
诀窍