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提供了模块化的搜索.它的特 ...
随机推荐
- Python 调用阿里云 API 收集 ECS 数据
#!/usr/bin/env python # coding: utf-8 # author: Wang XiaoQiang ''' 功能介绍: 1.调用阿里云API,收集所有区域 ECS 信息 2. ...
- nginx 里设置font-face 跨域
server { ... # Fix @font-face cross-domain restriction in Firefox location ~* \.(eot|ttf|woff)$ { ad ...
- <转>UNIX 共享内存应用中的问题及解决方法
http://www.ibm.com/developerworks/cn/aix/library/au-cn-sharemem/ 共享内存是一种非常重要且常用的进程间通信方式,相对于其它IPC机制,因 ...
- fork和vfork,exec
一.fork:子进程是父进程的一个拷贝,子进程获得同父进程相同的数据,但是同父进程使用不同的数据段和堆栈段. 特点:调用一次,返回两次.成功则在父进程中返回子进程ID,在子进程中返回0.失败则返回-1 ...
- Linux实战教学笔记23:Inotify事件监控工具
第二十三节 inotify事件监控工具 标签(空格分隔): Linux实战教学笔记-陈思齐 ---本教学笔记是本人学习和工作生涯中的摘记整理而成,此为初稿(尚有诸多不完善之处),为原创作品,允许转载, ...
- delete属性
- css实现栅格的方法
1. 方法一 1.1. 效果 2. 方法二 2.1. 效果 3. 代码 3.1. Html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 T ...
- java-tip-关于StringBuilder的使用
当我们需要拼接字符串时,通常会使用StringBuilder,这里简单分析下StringBuilder的内部结构. StringBuilder内部是一个char数组,当调用append方法连接字符串时 ...
- 14-敌兵布阵(HDU1166线段树 & 树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=1166 敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- 无网络安装mysql步骤
1. 先安装Microsoft Visual C++ 2010 运行环境,运行vcredist_x86.exe文件: 2. 安装MySql数据库,运行mysql-installer-community ...