出現的錯誤
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