報錯:AssertionError: `basename` argument not specified, and could not automatically determine the name from the viewset, as it does not have a `.queryset` attribute.


寫一些公共接口時,報錯:

經排查發現:

當viewset中沒有定義queryset字段時在路由的注冊必須加上basename:
例如:在views.py這種,定義了queryset字段

class PwdConfigViewSet(component_viewsets.ModelViewSet):

    queryset = PwdConfig.objects.filter() #定義了queryset字段
    ....
    .....

在urls.py中便是如下的寫法:

router = routers.DefaultRouter(trailing_slash=True)
router.register(r'pwd_config', views.PwdConfigViewSet)

如果在views.py中沒有定義queryset字段:

class CommonViewSet(component_viewsets.ModelViewSet):
    serializer_class = serializers.Serializer
    ......
    ......

則在urls.py中,需要加上basename:

router = routers.DefaultRouter(trailing_slash=True)
router.register(r'common', views.CommonViewSet, basename='common')


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM