天天看点

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

           

继续阅读