django drf GenericAPIView和ListAPIView
drf提供了更快捷的查询方法ListModelMixin+GenericAPIView,和ListAPIView
1.ListModelMixin+GenericAPIView
from django.shortcuts import render from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.parsers import JSONParser
from rest_framework import mixins, generics
from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination
from goods.models import Goods
from goods.serializer import GoodsSerializer 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) # 需要配置setting.py中的REST_FRAMEWORK节点
class GoodsList(ListAPIView):
queryset = Goods.objects.all()[:10]
serializer_class = GoodsSerializer
2.ListAPIView
from django.shortcuts import render from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.parsers import JSONParser
from rest_framework import mixins, generics
from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination
from goods.models import Goods
from goods.serializer import GoodsSerializer class GoodsList(ListAPIView):
class GoodsPagination(PageNumberPagination):
page_size = 1
page_size_query_param = 'pageSize'
page_query_param = 'p'
max_page_size = 100 queryset = Goods.objects.all()[:10]
serializer_class = GoodsSerializer
pagination_class = GoodsPagination
我们可以从源码中看到ListAPIVIew继承了ListModelMixin+GenericAPIView
django drf GenericAPIView和ListAPIView的更多相关文章
- django DRF理解
django restframework(DRF) 最近的开发过程当中,发现restframework的功能很强大,所以尝试解读了一下源码,写篇博客分享给大家,有错误的地方还请各位多多指出 视图部分 ...
- 解决Django + DRF:403 FORBIDDEN:CSRF令牌丢失或不正确,{"detail":"CSRF Failed: CSRF cookie not set."}
我有一个Android客户端应用程序尝试使用Django + DRF后端进行身份验证.但是,当我尝试登录时,我收到以下响应: 403: CSRF Failed: CSRF token missing ...
- Django DRF 分页
Django DRF 分页 分页在DRF当中可以一共有三种,可以通过setttings设置,也可也通过自定义设置 PageNumberPagination 使用URL http://127.0.0.1 ...
- django drf 级联数据和RetrieveModelMixin
1.定义View from django.shortcuts import render from rest_framework.views import APIView from rest_fram ...
- django drf 基础学习3
一 简述 这里来谈下一些基本原理 二 汇总 1 restful规范 1 根据method不同做不同的操作 request.method=' get(获取) 返回完整 ...
- django drf 基础学习2
DRF基本程序调用一 models初步编写 1 编写model.py from django.db import models 导入 class dbinfo(models.Model) ...
- django drf 基础学习1
一 环境配置 python3.5+ django2.0 pymysql二 安装 /usr/bin/python3 -m pip install django /usr/bin/pytho ...
- [django]drf知识点梳理-搜索
什么是搜索? 譬如http://127.0.0.1:8000/User/?username=maotai-0 可以检索出想要的. 自己实现原始的搜索 重写下get_queryset方法 class U ...
- Django + DRF + Elasticsearch 实现搜索功能
django使用haystack来调用Elasticsearch搜索引擎 如何使用django来调用Elasticsearch实现全文的搜索 Haystack为Django提供了模块化的搜索.它的特 ...
随机推荐
- 「小程序JAVA实战」小程序上传短视频(46)
转自:https://idig8.com/2018/09/14/xiaochengxujavashizhanxiaochengxushangchuanduanshipin45/ 个人信息:用户上传短视 ...
- c++builder自定义控件
c++builder自定义控件 http://docwiki.embarcadero.com/CodeExamples/XE8/en/RegisterComponents_%28C%2B%2B%29 ...
- 自动删除 Elasticsearch 索引
#!/bin/bash # author: Wang XiaoQiang# crontab -e# 0 0 * * * /root/script/del_esindex.sh # auto delet ...
- Writing Surface Shaders
[Writing Surface Shaders] Writing shaders that interact with lighting is complex. There are differen ...
- linux下echo与time服务的程序实现
一.针对ECHO服务的TCP客户软件的实现 1.网络拓扑结构: 2.源码: #include <stdio.h> #include <stdlib.h> #include &l ...
- 高性能Web服务器Nginx的配置与部署研究(2)Nginx入门级配置与部署及“Hello World”
1. Nginx 程序包 目前最新的开发版本时1.1.12: Linux/Unix:nginx-1.1.12.tar.gz Windows:nginx-1.1.12.zip 我们可以下载稳定版尝试: ...
- android用户登录验证
转自https://www.cnblogs.com/android-blogs/p/5912585.html
- Jquery循环select标签,并给指定option添加select属性后在页面上不显示的问题
<select id="testId"> <option value="">--请选择--</option> <opt ...
- phpmailer配置163邮箱
function send_email($email = ''){ $this->autoRender = false; date_default_timezone_set('PRC'); re ...
- Linux编程实现蜂鸣器演奏康定情歌
Linux编程实现蜂鸣器演奏康定情歌 摘自:https://blog.csdn.net/jiazhen/article/details/3490979 2008年12月10日 15:40:00 j ...