gpt4 book ai didi

python - “ListSerializer”对象不可调用

转载 作者:太空狗 更新时间:2023-10-29 22:29:23 25 4
gpt4 key购买 nike

我正在尝试为具有 content_type 对象的投票应用程序实现 Django-Rest 框架。我尝试使用 rest-framework-generic-relations对于 serializers.py。在我看来,错误可能在 serializer.py/views.py 中,但我是这个框架的新手,非常感谢您的帮助!

views.py:

class vote_detail(generics.RetrieveUpdateDestroyAPIView):
queryset = VotedItem.objects.all()
serializer_class = VoteSerializer(queryset, many=True)

序列化程序.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')

和模型.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

当我访问 www.mypage.com/likes/api/1/时,我收到一条错误消息:

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

最佳答案

错误是它试图调用 serializer_class,它是 ListSerializer 的实例,而不是类。

不应该这样:

serializer_class = VoteSerializer(queryset, many=True)

是这样的:

serializer_class = VoteSerializer

关于python - “ListSerializer”对象不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37578159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com