rest_framework的mixins, viewsets使用
views文件中,如goods的views中:
from rest_framework import mixins, viewsets
class GoodsListViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
"""
List all goods.
"""
queryset = Goods.objects.all()
serializer_class = GoodsSerializer
在urls文件中添加
# 使用router添加url 第1步
from rest_framework.routers import DefaultRouter
# 使用router添加url 第2步
router = DefaultRouter()
router.register(r'goods', GoodsListViewSet)
# 使用router添加url 第3步,在
urlpatterns = [
url(r'^', include(router.urls)),
]

浙公网安备 33010602011771号