我是靠谱客的博主 勤奋诺言,这篇文章主要介绍AssertionError: `base_name` argument not specified, and could not automatically determine the...,现在分享给大家,希望可以做个参考。
出现的错误
AssertionError: base_name argument not specified, and could not automatically determine the name from the viewset, as it does not have a .queryset attribute.
原因
在views中去掉了queryset属性,改用get_queryset()方法。
class GoodsViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
"""
商品列表页
"""
# queryset = Goods.objects.all()
serializer_class = GoodsSerializer
pagination_class = GoodsPagination
def get_queryset(self):
return Goods.objects.filter(shop_price__gt=100)
解决
router注册url时,增加base_name
router = DefaultRouter()
# 配置goods的url
router.register(r'goods', GoodsViewSet, base_name='') # views中取消queryset属性,采用get_queryset()方法,则必须加这个base_name
最后
以上就是勤奋诺言最近收集整理的关于AssertionError: `base_name` argument not specified, and could not automatically determine the...的全部内容,更多相关AssertionError:内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复