报错: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