django ttributeError:类型对象“"没有属性”get_extra_actions“

jei2mxaa  于 2023-11-20  发布在  Go
关注(0)|答案(2)|浏览(241)

我是Django的新手,对我所面临的错误完全困惑。我试图创建一个rest API并应用Django过滤选项。
这是我的视图代码。

@permission_classes([AllowAny])
class op_listView(generics.ListAPIView):
    serializer_class = op_Serializer
    queryset = Op.objects.all()
    filter_backends = (DjangoFilterBackend,OrderingFilter, SearchFilter)
    filter_fields=('website', 'organization__name', 'sectors__name','countries__name')

字符串
我的模型

class Op(models.Model):
    website = models.CharField(max_length=40)
    op_link = models.CharField(null=True, max_length=200)
    title = models.TextField(null=True)
    description = models.TextField(null=True)
    organization = models.ForeignKey(Organization, on_delete=models.DO_NOTHING)
    close_date = models.DateField(null=True)
    created_at = models.DateTimeField(auto_now_add=True, null=True)
    checksum = models.CharField(max_length=200, null=True)
    relevancy = models.IntegerField(null=True)
    mark_relevant = models.IntegerField(null=True)
    published_date = models.DateField(null=True)
    language = models.ForeignKey(Language, on_delete=models.DO_NOTHING, default=105)
    status = models.IntegerField(null=True, default=0)
    read_status = models.IntegerField(default=0)

    @property
    def attachments(self):
        return self.attachments_set.all()

    @property
    def countries(self):
        return self.country_set.all()

    @property
    def sectors(self):
        return self.sector_set.all()

    class Meta:
        db_table = "op"


我的序列化器

class op_Serializer(serializers.ModelSerializer) :
    attachments = attachments_Serializer(many=True , required=False)
    countries = country_Serializer(many=True , required=False)
    sectors = sector_Serializer(many=True , required=False)

    class Meta :
        model = Op
        fields = ['id', 'website', 'op_link', 'title', 'description' , 'organization' , 'close_date' ,
                  'created_at', 'relevancy', 'published_date', 'language' , 'status' , 'read_status' ,
                  'attachments', 'countries', 'sectors']
        depth = 2


网址

from . import views
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register('op-filter', views.op_listView, basename='op-filter')
urlpatterns = router.urls


我的代码不工作,并说,

extra_actions = viewset.get_extra_actions()
AttributeError: type object 'op_listView' has no attribute 'get_extra_actions


谁能帮我解决这个问题?非常感谢你的帮助。

vwkv1x7d

vwkv1x7d1#

为了让它工作,使用path,而不是url或register,我使用了path,它工作得很好,例如使用,path('api/register',RegisterAPI,'register'),

k2arahey

k2arahey2#

我通过改变我的视图集解决了这个错误:

class TestRunGetView(generics.RetrieveAPIView):
    """ View to get details of one test job run. """
    permission_classes = [HasAPIKey | IsAuthenticated]
    queryset = TestRun.objects.all()
    serializer_class = TestRunSerializer

字符串
对此:

class TestRunGetView(viewsets.GenericViewSet,
                     viewsets.mixins.CreateModelMixin):
    """ View to get details of one test job run. """
    permission_classes = [HasAPIKey | IsAuthenticated]
    queryset = TestRun.objects.all()
    serializer_class = TestRunSerializer


urls.py中也可以这样做:

from rest_framework.routers import DefaultRouter
from myapp.views import TestRunGetView

router = DefaultRouter()
router.register('api/testrun', TestRunGetView, basename='testrun')

相关问题