随笔分类 -  django rest framework

摘要:#测试APIView的request from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status from app.mo 阅读全文
posted @ 2020-09-25 20:06 亦木121 阅读(130) 评论(0) 推荐(0)
摘要:#测试APIView的request from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status from app.mo 阅读全文
posted @ 2020-09-25 16:50 亦木121 阅读(105) 评论(0) 推荐(0)
摘要:GenericAPIView 1、继承自APIView,为列表视图和详情视图添加了常用的行为和属性 1)行为(方法) get_queryset:获取queryset的数据集 get_serializer:获取serializer_class序列化器对象 get_object:根据lookup_fie 阅读全文
posted @ 2020-09-25 10:36 亦木121 阅读(171) 评论(0) 推荐(0)
摘要:urls.py的配置 from django.conf.urls import url,include from django.contrib import admin from app import views from django.urls import path urlpatterns = 阅读全文
posted @ 2020-09-18 16:38 亦木121 阅读(530) 评论(0) 推荐(0)
摘要:APIView实现列表视图 #测试APIView的request from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import stat 阅读全文
posted @ 2020-09-18 15:01 亦木121 阅读(132) 评论(0) 推荐(0)
摘要:1、继承自view 2、提供了rest_framework自己的request对象,不是django里的HttpRequest对象 3.返回了est_framework自己的response队形,不时django里的HttpResponset对象 4.并且提供了权限、认证、限流等功能 View获取数 阅读全文
posted @ 2020-09-18 09:35 亦木121 阅读(163) 评论(0) 推荐(0)
摘要:模型类的定义 from django.db import models # Create your models here. class BookInfo(models.Model): btitle = models.CharField(max_length=20,verbose_name='名称' 阅读全文
posted @ 2020-09-17 20:31 亦木121 阅读(171) 评论(0) 推荐(0)
摘要:1、Modelserializer会根据模型类自动生成字段 2、还可以手动添加字段 3、提供了反序列化时用的create方法和update方法,不需要再手动在序列化器中创建create方法 from rest_framework import serializers from app.models 阅读全文
posted @ 2020-09-17 20:02 亦木121 阅读(196) 评论(0) 推荐(0)
摘要:反序列化:json数据转换成模型类数据(校验、入库) 反序列化的校验: 1、字段类型校验 2、字段选项校验 3、单字段校验(方法) 4、多字段校验(方法) 5、自定义校验(方法) 反序列的入库: 1、创建新的对象create 2、更新现有的对象update '''序列化器反序列化书籍对象''' #1 阅读全文
posted @ 2020-09-17 17:38 亦木121 阅读(252) 评论(0) 推荐(0)
摘要:定义好序列化器后,视图类的写法 from app.models import BookInfo from app.serializers import BookInfoSerializer from django.views import View from django import http i 阅读全文
posted @ 2020-09-17 13:33 亦木121 阅读(177) 评论(0) 推荐(0)
摘要:书籍序列化器,关联many 首先,书籍模型中不含有外键,而英雄模型中含有书籍的外键。 外键的定义: hbook = models.ForeignKey(BookInfo,on_delete=models.CASCADE,verbose_name='图书') 那么在模型里,知道了某个英雄,通过下面得到 阅读全文
posted @ 2020-09-17 11:07 亦木121 阅读(161) 评论(0) 推荐(0)
摘要:根据下面英雄模型类定义序列化器 #定义英雄模型类 class HeroInfo(models.Model): GENDER_CHOICES = ( (0,'female'), (1,'male') ) hname = models.CharField(max_length=20,verbose_na 阅读全文
posted @ 2020-09-16 23:22 亦木121 阅读(225) 评论(0) 推荐(0)
摘要:1.序列化单个对象 '''1,序列化器,序列化单个书籍对象''' from app.models import BookInfo from app.serializers import BookInfoSerializer #1.获取数据 book = BookInfo.objects.get(id 阅读全文
posted @ 2020-09-16 20:38 亦木121 阅读(115) 评论(0) 推荐(0)
摘要:序列化器的作用: 1.序列化 2.反序列化 3.数据的校验和转换 定义序列化器的步骤: 1.定义类,继承自Serializer 2.和模型类,字段名字一样 3.和模型类,字段类型一样 4.和模型类,字段选项一样 from rest_framework import serializers #定义序列 阅读全文
posted @ 2020-09-16 18:00 亦木121 阅读(123) 评论(0) 推荐(0)
摘要: 阅读全文
posted @ 2020-09-16 17:53 亦木121 阅读(134) 评论(0) 推荐(0)
摘要:注意:以上4种请求一共需要创建两个视图类 以上不带参数pk的为列表视图,带pk的为详情视图 1.列表视图:获取所有书籍 步骤: 1)查询所有数据 2)转换数据 3)返回响应 2.列表视图:创建单本书籍post方法 步骤: 1)获取参数 2)校验参数 3)数据入库 4)返回响应 3.详情视图:获取单本 阅读全文
posted @ 2020-09-16 15:44 亦木121 阅读(739) 评论(0) 推荐(0)
摘要:1.settings中注册rest_framework 2.settings中注册子应用 INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django. 阅读全文
posted @ 2020-09-16 15:10 亦木121 阅读(563) 评论(0) 推荐(0)
摘要:from django.db import models # Create your models here. class BookInfo(models.Model): btitle = models.CharField(max_length=20,verbose_name='名称') bpub_ 阅读全文
posted @ 2020-09-16 14:35 亦木121 阅读(169) 评论(0) 推荐(0)
摘要: 阅读全文
posted @ 2020-09-16 14:02 亦木121 阅读(136) 评论(0) 推荐(0)