from .models import Goods
from goods.serializers import GoodsSerializer
from rest_framework import mixins
from rest_framework import generics
# Create your views here.
class GoodsList(mixins.ListModelMixin, generics.GenericAPIView):
"""
商品列表页
"""
queryset = Goods.objects.all()[:10]
serializer_class = GoodsSerializer
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
写完之后我们就可以重启项目刷新页面、我们可以看到数据已经正常返回了。
既然GenericAPIView功能这么强大、那还有没有更加简洁的方法呢?实际上GenericAPIView为我们提供了很多方法,比如:CreateAPIView,ListAPIView,RetrieveAPIView,DestroyAPIView,UpdateAPIView等等。我们可以直接继承这些方法即可。
from .models import Goods
from goods.serializers import GoodsSerializer
from rest_framework import mixins
from rest_framework import generics
# Create your views here.
class GoodsList(generics.ListAPIView):
"""
商品列表页
"""
queryset = Goods.objects.all()[:10]
serializer_class = GoodsSerializer
It is not my first time to pay a visit this web site, i am browsing this site dailly and obtain nice data from here all the time. Manon Chrissie Mini
Hello, I want to subscribe for this weblog to take latest updates, therefore where can i do it please help. Rafaelia Arnold Georgianna
I was reading through some of your blog posts on this internet site and I think this web site is real informative ! Retain posting . Peggie Goran Rosecan
I am sure this paragraph has touched all the internet people, its really really fastidious paragraph on building up new website. Tasia Leonhard Lange