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

推荐文章

51条评论

  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

  17. Fabulous, what a web site it is! This web site gives helpful data to us, keep it
    up.

  18. What’s up to all, it’s really a good for me to pay a visit this site, it includes precious Information.

  19. There is certainly a great deal to find out about this issue.
    I love all of the points you made.

  20. This is very interesting, You’re a very skilled blogger. I have
    joined your feed and look ahead to looking for extra of your magnificent post.
    Additionally, I have shared your website in my social networks

  21. Greetings! Very helpful advice within this article!
    It is the little changes that will make the biggest changes.
    Many thanks for sharing!

  22. My developer is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the costs.

    But he’s tryiong none the less. I’ve been using Movable-type
    on several websites for about a year and am concerned about switching to another platform.
    I have heard excellent things about blogengine.net.
    Is there a way I can import all my wordpress posts into it?
    Any kind of help would be greatly appreciated!

  23. Hello my loved one! I want to say that this post is awesome, great written and include approximately all vital infos.
    I’d like to peer more posts like this .

  24. Heya i am for the primary time here. I found this
    board and I to find It truly helpful & it helped me out a lot.
    I am hoping to present one thing again and aid others like you aided
    me.

  25. What you wrote made a ton of sense. But, think on this, suppose you were to create a awesome post title?

    I am not saying your information isn’t good., but what if you added something that makes people want more?
    I mean DRF序列化之GenericView – 邹坤个人博客 is kinda boring.

    You could look at Yahoo’s front page and note how they create news headlines to grab people to open the
    links. You might add a related video or a picture or two to get readers excited
    about everything’ve got to say. In my opinion, it could bring your website a little
    livelier.

  26. Hello, after reading this remarkable post i am also delighted to share my know-how here with colleagues.

  27. I really like what you guys are usually up too. This sort of clever work and reporting!

    Keep up the awesome works guys I’ve you guys to my blogroll.

  28. Hello there, You have done an excellent job. I’ll certainly digg it and personally suggest to my friends.
    I am sure they will be benefited from this website.

  29. I know this if off topic but I’m looking into starting my own blog and was curious what all is required to get set up?

    I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very internet savvy so I’m not 100% positive.
    Any recommendations or advice would be greatly appreciated.
    Thank you

  30. Hi there! Someone in my Facebook group shared this
    site with us so I came to take a look. I’m definitely loving the
    information. I’m bookmarking and will be tweeting this to my followers!
    Fantastic blog and excellent design.

  31. First off I want to say excellent blog! I had a quick question in which I’d like to ask if you do
    not mind. I was interested to find out how you center yourself and clear your mind before writing.
    I have had a difficult time clearing my mind in getting my ideas out there.

    I do enjoy writing but it just seems like the first 10 to 15 minutes are
    usually wasted simply just trying to figure out how to begin.
    Any recommendations or tips? Cheers!

  32. With havin so much content and articles do you ever run into any problems of plagorism or copyright infringement?
    My site has a lot of exclusive content I’ve either created myself or outsourced but it looks like a lot of it is popping it up all over the internet
    without my permission. Do you know any solutions to help protect against content from being ripped off?
    I’d genuinely appreciate it.

  33. Link exchange is nothing else except it is just
    placing the other person’s webpage link on your page at suitable place and other person will also do similar in favor of
    you.

  34. fantastic submit, very informative. I’m wondering why the opposite experts of this sector do not notice this.
    You should continue your writing. I am sure, you
    have a huge readers’ base already!

  35. This excellent website really has all the information I wanted about
    this subject and didn’t know who to ask.

  36. WOW just what I was searching for. Came here by searching for Leke Kremi

  37. Pretty nice post. I just stumbled upon your weblog and wished to say that I have truly enjoyed surfing around your blog posts.

    After all I’ll be subscribing to your rss feed and I hope you write again very
    soon!

  38. My family always say that I am wasting my time here at net, except
    I know I am getting experience all the time
    by reading thes fastidious articles.

  39. I blog frequently and I truly appreciate your information. This great
    article has truly peaked my interest. I am going to book mark
    your blog and keep checking for new details about once a week.
    I opted in for your Feed as well.

  40. I constantly spent my half an hour to read this website’s articles all
    the time along with a cup of coffee.

  41. My coder is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the expenses.
    But he’s tryiong none the less. I’ve been using Movable-type on numerous websites for about a year and am
    anxious about switching to another platform.
    I have heard very good things about blogengine.net. Is there
    a way I can transfer all my wordpress content into
    it? Any kind of help would be greatly appreciated!

  42. you are best, very nicee blog your amazing site.

  43. you are besst, very nicee blog your amazing site.

  44. yoou are best, very nicee blog your ammazing site.

  45. Your articles and comments are really nicee. We wish you luck. good day.

  46. Excellent post. I used to be checking continuously this weblog and
    I’m inspired! Very helpful info specifically the remaining phase :
    ) I handle such information much. I was looking for this certain info for
    a very long time. Thank you and best of luck.

  47. I am not sure where you’re getting your info, but great topic.
    I needs to spend some time learning much more or understanding more.
    Thanks for great information I was looking
    for this info for my mission.

  48. I was suggested this web site by my cousin. I’m not sure whether this post is written by him as nobody else know such detailed about my difficulty.
    You are amazing! Thanks!

  49. I really like the sttatuses and articles you share and your blogs.

发表评论

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