14

I am trying to implement Django-Rest framework for a voting application with content_type objects. I tried using the rest-framework-generic-relations for serializers.py. It seems to me that the error might be within serializer.py / views.py, but I am new to this framework and would appreciate your 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
from .models import VotedItem
from posts.models import Post
from generic_relations.relations import GenericRelatedField

class PostSerializer(serializers.ModelSerializer):
    class Meta:
        model = Post
        fields = ('title',)

class VoteSerializer(serializers.ModelSerializer):
        """
        A `VotedItem` serializer with a `GenericRelatedField` mapping all possible
        models to their respective serializers.
        """
    voted_object = GenericRelatedField({
        Post: PostSerializer(),
    })

    class Meta:
        model = VotedItem
        fields = ('user', 'voted_object')

and models.py:

from __future__ import unicode_literals

from datetime import datetime

from django.utils.encoding import python_2_unicode_compatible
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
from django.utils import timezone
from django.contrib.auth.models import User
from social_count.managers import VoteManager

SCORES = (
    (+1, '+1'),
    (-1, '-1'),
)

@python_2_unicode_compatible
class VotedItem(models.Model):
    """
    A vote on an object by a 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'
        # One 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 I visit www.mypage.com/likes/api/1/ I get an 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 get
  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 at /likes/api/1/
Exception Value: 'ListSerializer' object is not callable
Henry H
  • 612
  • 2
  • 8
  • 19

1 Answers1

28

The error is that it's trying to call the serializer_class, which is an instance of ListSerializer, instead of a clas.

Shouldn't this:

serializer_class = VoteSerializer(queryset, many=True)

Be that:

serializer_class = VoteSerializer
jpic
  • 32,891
  • 5
  • 112
  • 113
  • 1
    This error is triggered after changing to your suggestion: AttributeError at /likes/api/1/ Got AttributeError when attempting to get a value for field `voted_object` on serializer `VoteSerializer`. The serializer field might be named incorrectly and not match any attribute or key on the `VotedItem` instance. Original exception text was: 'VotedItem' object has no attribute 'voted_object'. Thanks for your help though. – Henry H Jun 01 '16 at 21:11
  • The answer above is correct. You should mark it as such and start a new question. – Linovia Jun 02 '16 at 11:11
  • I'm getting this error for a similar reason; however, I need to explicitly set `many=True` ([link](https://stackoverflow.com/questions/55041434/original-exception-text-was-queryset-object-has-no-attribute-weight) to why) in order to use a multiple lookup_fields mixin ([link](https://stackoverflow.com/questions/38461366/multiple-lookup-fields-for-django-rest-framework)). Is this not possible? – Bryton Beesley May 20 '21 at 01:07
  • 2
    This solution does not solve the problem when trying to create multiple objects in 1 request. The same error appears. It seems that some functions have to be overriden in both the view and serializer as described in this DRF issue https://github.com/encode/django-rest-framework/issues/5502 – Seb Oct 04 '21 at 09:37