AttributeError:类型对象“ ListViewSet”没有属性“ get_extra_actions”

时间:2018-10-18 16:34:10

标签: python django django-rest-framework

我正在尝试构建一个基本上具有卡片和列表的scrumboard django应用程序。我无法解决以下问题。

Unhandled exception in thread started by <function check_errors 
<locals>.wrapper
at 0x0000000003DF7C80>
Traceback (most recent call last):
File "C:\Users\...\lib\site- 
packages\django\utils\autoreload.py",
line 225, in wrapper
fn(*args, **kwargs)

...
urlpatterns = router.urls
File "C:\Users\...\lib\site- 
packages\rest_framework\routers.py", line 101, in urls
    self._urls = self.get_urls()
File "C:\Users\...\lib\site- 
packages\rest_framework\routers.py", line 363, in get_urls
    urls = super(DefaultRouter, self).get_urls()
File "C:\Users\...\lib\site- 
packages\rest_framework\routers.py", line 261, in get_urls
    routes = self.get_routes(viewset)
File "C:\Users\...lib\site-packages\rest_framework\routers.py", 
line 176, in get_routes
    extra_actions = viewset.get_extra_actions()
AttributeError: type object 'ListViewSet' has no attribute 
'get_extra_actions'

以下是我的模特。py

from django.db import models
class  List(models.Model):
    name = models.CharField(max_length=50)
    def __str__(self):
        return "List : {}".format(self.name) # returns list values

class Card(models.Model): 
    title = models.CharField(max_length=100)
    description = models.TextField(blank=True) 
    list = models.ForeignKey(
        List, 
        related_name = "card",
        on_delete=models.PROTECT
    ) 
    story_points = models.IntegerField(null=True, blank = True)
    business_value = models.IntegerField(null=True, blank = True)
    def __str__(self):
        return "Card : {}".format(self.title)

定义视图集的api.py

from rest_framework.viewsets import ModelViewSet
from drf_multiple_model.views import ObjectMultipleModelAPIView
from .serializers import ListSerializer, CardSerializer
from .models import List, Card

class ListViewSet(ModelViewSet):
    queryset = List.objects.all()
    serializer_class = ListSerializer


class CardViewSet(ModelViewSet):
    queryset = Card.objects.all()
    serializer_class = CardSerializer

class ListViewSet(ObjectMultipleModelAPIView):
    querylist = [
        {
            'queryset': List.objects.all(), 
            'serializer_class': ListSerializer
        },
        {
            'queryset': Card.objects.all(), 
            'serializer_class': CardSerializer
        }
    ]

其中是例外的urls.py文件。

from .api import ListViewSet, CardViewSet
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register(r'lists', ListViewSet, base_name='List')
router.register(r'cards', CardViewSet, base_name='Card')
urlpatterns = router.urls

我是django的新手,我正在教程的帮助下进行开发。我一直在尝试找到解决方案,但是我对解决方案不太满意,因此将其发布在这里。

非常感谢。

编辑: serializers.py文件

from rest_framework import serializers
from .models import List, Card

class CardSerializer(serializers.ModelSerializer):
    class Meta:
        model = Card
        fields = '__all__'

class ListSerializer(serializers.ModelSerializer):
    class Meta:
        model = List
        fields = '__all__'

版本, 的Django == 2.1.2 djangorestframework == 3.8.2

1 个答案:

答案 0 :(得分:1)

我认为最可能的原因是django-rest-multiple-models ObjectMultipleModelAPIView不是ViewSet的子类,它是GenericAPIView的子类,而{{1} }方法是在其他框架get_extra_actions类(https://github.com/encode/django-rest-framework/blob/3.8.2/rest_framework/viewsets.py#L146)上定义的

您应该将ViewSetMixin更新为ObjectMultipleModelAPIView,希望它可以与ObjectMultipleModelAPIViewSet一起使用