python - 'ListSerializer' object is not callable -
i trying implement django-rest framework voting application content_type objects. tried using rest-framework-generic-relations serializers.py. seems me error might within serializer.py / views.py, new framework , appreciate help!
views.py:
class vote_detail(generics.retrieveupdatedestroyapiview): queryset = voteditem.objects.all() serializer_class = voteserializer(queryset, many=true)
serializers.py:
from rest_framework import serializers .models import voteditem posts.models import post generic_relations.relations import genericrelatedfield class postserializer(serializers.modelserializer): class meta: model = post fields = ('title',) class voteserializer(serializers.modelserializer): """ `voteditem` serializer `genericrelatedfield` mapping possible models respective serializers. """ voted_object = genericrelatedfield({ post: postserializer(), }) class meta: model = voteditem fields = ('user', 'voted_object')
and models.py:
from __future__ import unicode_literals datetime import datetime django.utils.encoding import python_2_unicode_compatible django.db import models django.contrib.contenttypes.fields import genericforeignkey, genericrelation django.contrib.contenttypes.models import contenttype django.core.urlresolvers import reverse django.utils import timezone django.contrib.auth.models import user social_count.managers import votemanager scores = ( (+1, '+1'), (-1, '-1'), ) @python_2_unicode_compatible class voteditem(models.model): """ vote on object user. """ user = models.foreignkey(user) content_type = models.foreignkey( contenttype, null=true, blank=true, on_delete=models.cascade, ) object_id = models.positiveintegerfield( null=true, ) content_object = genericforeignkey('content_type', 'object_id') vote = models.smallintegerfield(choices=scores, blank=true, null=true) flag = models.nullbooleanfield(blank=true, null=true) timestamp = models.datetimefield(auto_now=false, auto_now_add=true) objects = votemanager() class meta: db_table = 'votes' # 1 vote per user per object unique_together = (('user', 'content_type', 'object_id'),) def __str__(self): return '%s: %s on %s' % (self.user, self.vote, self.content_object) def is_upvote(self): return self.vote == 1 def is_downvote(self): return self.vote == -1
when visit www.mypage.com/likes/api/1/ error message:
traceback: file "/home/henry/newpine/src/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 149. response = self.process_exception_by_middleware(e, request) file "/home/henry/newpine/src/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 147. response = wrapped_callback(request, *callback_args, **callback_kwargs) file "/home/henry/newpine/src/env/local/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view 58. return view_func(*args, **kwargs) file "/home/henry/newpine/src/env/local/lib/python2.7/site-packages/django/views/generic/base.py" in view 68. return self.dispatch(request, *args, **kwargs) file "/home/henry/newpine/src/env/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch 466. response = self.handle_exception(exc) file "/home/henry/newpine/src/env/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch 463. response = handler(request, *args, **kwargs) file "/home/henry/newpine/src/env/local/lib/python2.7/site-packages/rest_framework/generics.py" in 286. return self.retrieve(request, *args, **kwargs) file "/home/henry/newpine/src/env/local/lib/python2.7/site-packages/rest_framework/mixins.py" in retrieve 57. serializer = self.get_serializer(instance) file "/home/henry/newpine/src/env/local/lib/python2.7/site-packages/rest_framework/generics.py" in get_serializer 111. return serializer_class(*args, **kwargs) exception type: typeerror @ /likes/api/1/ exception value: 'listserializer' object not callable
the error it's trying call serializer_class, instance of listserializer, instead of clas.
shouldn't this:
serializer_class = voteserializer(queryset, many=true)
be that:
serializer_class = voteserializer