ReturnDict object not callable Django Rest Framework

I am new to both SO and Django DRF. Whenever I try running the code. I get the error "TypeError ReturnDict object not callable".

models.py

from __future__ import unicode_literals

from django.db import models

# Create your models here.

class Fish(models.Model):
    name=models.CharField(max_length=120)
    created=models.DateTimeField(auto_now_add=True)
    active = models.BooleanField()

serializers.py

from  fishes.models import  Fish
from  rest_framework import  serializers

class FishSerializers(serializers.ModelSerializer):
    success=serializers.SerializerMethodField('is_successful')



    def is_successful(self,obj):
        return self.context.get('value')

    class Meta:
        model=Fish
        fields=('name', 'active', 'created','success')

views.py

from django.shortcuts import render
from  rest_framework import viewsets
from rest_framework import permissions
from  fishes.serializers import FishSerializers
from fishes.models import  Fish
# Create your views here.

class FishViewSets(viewsets.ModelViewSet):
    queryset = Fish.objects.get(id=2)
    serializer_class = FishSerializers(queryset,context={'value':'brother'}).data

urls.py

from django.conf.urls import url,include
from django.contrib import admin
from rest_framework import  routers
from fishes import  views

router= routers.DefaultRouter()
router.register(r'api/fishes',views.FishViewSets,base_name='FishViewSets')
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^',include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

error_trace

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/viewsets.py", line 87, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py", line 466, in dispatch
    response = self.handle_exception(exc)
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py", line 463, in dispatch
    response = handler(request, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/mixins.py", line 47, in list
    serializer = self.get_serializer(queryset, many=True)
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/generics.py", line 111, in get_serializer
    return serializer_class(*args, **kwargs)
TypeError: 'ReturnDict' object is not callable

Please help to understand and resolve this error in django rest framework!

Answers


There are at least two problems is in your FishViewSets.

  1. queryset should be a queryset (you set a single instance)

  2. serializer_class has to be a class. You set it to a serialized instance of your serializer.

This should help:

class FishViewSets(viewsets.ModelViewSet):
    queryset = Fish.objects.all()
    serializer_class = FishSerializers

Need Your Help

Parse text to a CSV-file with PHP

php csv export-to-csv fputcsv

I want to parse text from my database into a CSV-file with PHP. The problem is that nothing is parsed to the file.

Robot Framework - Can't set Object for Attribute using Execute JavaScript

javascript selenium-webdriver robotframework

I want to set Object for value attribute using Execute Javascript but it doesn't work with robot framework (it work with C#)