DRF序列化之GenericView

前面我们通过ModelSerializer实现了复杂的嵌套式序列化。是不是DRF就只有这一点功能呢?我们的代码还能不能再简洁一点呢?答案是肯定是、哈哈哈(为什么每次答案都是肯定的?)我们不再使用APIView来实现我们上面讲到的功能。我们会使用一些DRF更加高级的View方法:GenericView里面的mixins.ListModelMixin以及generics.GenericAPIView。GenericAPIView是一个非常重要的View、他在APIView的基础上进行了二次封装、帮助我们实现了很多功能、比如:queryset,serializer_class,filter_backends,pagination_class等等。

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)

写完之后我们就可以重启项目刷新页面、我们可以看到数据已经正常返回了。

img

既然GenericAPIView功能这么强大、那还有没有更加简洁的方法呢?实际上GenericAPIView为我们提供了很多方法,比如:CreateAPIView,ListAPIView,RetrieveAPIView,DestroyAPIView,UpdateAPIView等等。我们可以直接继承这些方法即可。

img

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

img

推荐文章

17条评论

  1. 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

  2. Hello, I want to subscribe for this weblog to take latest updates, therefore where can i do it please help. Rafaelia Arnold Georgianna

  3. 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

  4. I am sure this paragraph has touched all the internet people, its really really fastidious paragraph on building up new website. Tasia Leonhard Lange

  5. Hello. This article was extremely motivating, especially since I was searching for thoughts on this matter last Tuesday. Chloette Giff Idaline

  6. There is noticeably a lot to realize about this. I suppose you made various good points in features also. Audrye Schuyler Weixel

  7. You have made some good points there. I looked on the web for more information about the issue and found most individuals will go along with your views on this web site. Rochell Fonz Gilcrest

  8. I absolutely love your site.. Pleasant colors & theme. Mignonne Abram Ciccia

  9. This paragraph is genuinely a nice one it helps new internet visitors, who are wishing for blogging. Belva Rinaldo Ha

  10. Awsome site! I am loving it!! Will be back later to read some more. I am taking your feeds also. Catharina Shem Darelle

  11. I have read so many posts concerning the blogger lovers however this article is genuinely a pleasant piece of writing, keep it up.| Valerie Aldon Kidd

  12. Great blog. Really looking forward to read more. Really Great. Charmian Hershel Alysa

  13. Thank you for another informative website.
    The place else may just I get that kind of info written in such
    a perfect manner? I have a mission that I’m simply now
    working on, and I have been at the glance out for such information.

  14. I am not certain where you’re getting your info, but great
    topic. I needs to spend some time finding out much more or working
    out more. Thank you for excellent info I used to be in search of
    this info for my mission.

  15. I’d like to find out more? I’d like to find out some additional information.

  16. “I’d like to thank you for the efforts you have put in penning this site. I am hoping to see the same high-grade blog posts by you in the future as well. In fact, your creative writing abilities has encouraged me to get my own blog now ;)”
    456854

发表评论

邮箱地址不会被公开。 必填项已用*标注