Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django restframework :getting NotImplementedError

I m getting a NotimplementError. I m working on Django restframework authentications and permissions. I m able to input data through admin panel but when is comes to serializers I m getting this error.

Below is the error report:

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

here is my 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

below is 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

below is 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

I m a beginner in Django and rest framework. I m following this tutorial. Now I m stuck here. please help.

Thanks!!!!

like image 958
Zeeshan Avatar asked Mar 08 '16 09:03

Zeeshan


1 Answers

Indeed, changing

owner = serializers.Field(source='owner.username')

to

owner = serializers.ReadOnlyField(source='owner.username')

does the trick

like image 98
David Avatar answered Sep 30 '22 15:09

David