写一些公共接口时,报错:
报错: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')

相关文章:

  • 2022-12-23
  • 2021-04-18
  • 2022-12-23
  • 2022-01-11
猜你喜欢
  • 2021-08-11
  • 2021-08-18
  • 2022-12-23
  • 2021-06-09
  • 2022-12-23
相关资源
相似解决方案