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提供了模块化的搜索.它的特 ...
随机推荐
- css style study
<img style="float:none;display:block;margin:0px auto" src="images/aa.jpg"> ...
- 搭建Git Server - Centos+Gitosis
参考并部分转载自:http://www.pfeng.org/archives/757 1. 安装依赖 yum -y install curl-devel expat-devel gettext-dev ...
- bash's [ command & [[ keyword
[bash's [ command & [[ keyword] [ (test) command: bash中的条件测试語句, [ condition ], 并不是一个語句, 而是一个命令, ...
- JavaScript怎么让字符串和JSON相互转化
var obj = str.parseJSON(); //由JSON字符串转换为JSON对象 var obj = JSON.parse(str); //由JSON字符串转换为JSON对象
- lnline Hook初试
简单解释下hook: 钩子(Hook),是Windows消息处理机制的一个平台,应用程序可以在上面设置子程以监视指定窗口的某种消息,而且所监视的窗口可以是其他进程所创建的.当消息到达后,在目标窗口处理 ...
- [poj2104]kth-number(归并树求区间第k大)
复杂度:$O(nlog^3n)$ #include<cstdio> #include<cstring> #include<algorithm> #include&l ...
- tomcat安装后,双击start.bat闪退的问题
1.jdk环境变量没有配 解决方案:我的电脑-属性-高级-环境变量,新增下面三个环境变量: ①JAVA_HOME=C:\Program Files\Java\jdk1.7.0_09(就是你jdk安装的 ...
- 集合_java集合框架
转载自http://blog.csdn.net/zsw101259/article/details/7570033 Java集合框架图 简化图: Java平台提供了一个全新的集合框架.“集合框架”主要 ...
- Boost::lexical_cast类型转换
1.字符串->数值 C++代码 #include <boost/lexical_cast.hpp> #include <iostream> int main() { us ...
- 黑客攻击 UVa11825
http://www.cnblogs.com/acm-bingzi/p/3272898.html Hackers’ Crackdown Miracle Corporations has a numbe ...