摘要: ORM多表操作: 连表: manager.select_related('fk1',..) # 一次连表获得所有数据 manager.prefetch_related('fk1',..) # 子查询, 两次查询一次缓存 1. 一对多 1>. 添加记录 方式一: 直接指定外键id book_info= 阅读全文
posted @ 2019-05-15 20:27 lancelotxly 阅读(145) 评论(0) 推荐(0) 编辑
摘要: ORM单表操作: 在views.py里面 from models import * t = Table() # 表单对象model是一个表记录, model只有attrs和save() manager = Table.objects # 表单的一个类属性是一个Manager对象, 用于管理表记录对象 阅读全文
posted @ 2019-05-15 20:21 lancelotxly 阅读(157) 评论(0) 推荐(0) 编辑
摘要: 1. 创建表: python manage.py makemigrations # 创建model迁移文件 python manage.py migrate # 迁移model至数据库生成具体表 2. 修改表, 重新跑一遍3. 删除表: 1>. 先到数据库把表删掉:drop table 2>. 注释 阅读全文
posted @ 2019-05-15 20:04 lancelotxly 阅读(2108) 评论(0) 推荐(0) 编辑
摘要: 一对多: 多的那方为子表, 设置外键 fk = models.ForeignKey( # django默认创建为 fk_id to='主表名', # 可不加to to_field='主表字段名', # 默认为主表的主键 on_delete='级联方式', # models.CASCADE // 父表 阅读全文
posted @ 2019-05-15 20:03 lancelotxly 阅读(1489) 评论(0) 推荐(0) 编辑
摘要: Model组件: (字段)作用: 1. 后端建表 2. 限制并验证admin输入的数据类型 使用: 在models.py中创建表 每一张表是一个类class, 表的每一项数据是这个类的对象obj class Table(model.Models): field = model.Field 1. Fi 阅读全文
posted @ 2019-05-15 20:00 lancelotxly 阅读(214) 评论(0) 推荐(0) 编辑
摘要: 自定义filter和tag: 1>. 自定义filter: 1>>. app中创建templatetags模块 2>>. 创建任意myTag.py文件 3>>. from django import template register = template.Library() #register的名 阅读全文
posted @ 2019-05-15 19:39 lancelotxly 阅读(142) 评论(0) 推荐(0) 编辑
摘要: 模板: Template + Context 1. 基本流程: from django.template import Template, Context from django.template.load import get_template 1>. 具体过程 # t = Template('< 阅读全文
posted @ 2019-05-15 19:37 lancelotxly 阅读(135) 评论(0) 推荐(0) 编辑
摘要: 搭建工作环境的流程: 1. 创建project 2. 创建app python manage.py startapp app01 3. 在settings.py中注册app app01.apps.AppConfig 4. 配置静态变量环境 #1 在app01中创建静态文件夹: static - cs 阅读全文
posted @ 2019-05-15 19:35 lancelotxly 阅读(138) 评论(0) 推荐(0) 编辑
摘要: 基本MVC流程: 1>. urls.py 中路由选择, 分配视图函数 (C) 2>. views.py 中具体实现逻辑 (V) 1>>. 返回render渲染的模板 return render(req, 'test.html', {'模板变量':'python变量'}) 2>>. 直接返回HTTPR 阅读全文
posted @ 2019-05-15 19:33 lancelotxly 阅读(152) 评论(0) 推荐(0) 编辑
摘要: url系统: urls.py 1>. 导入 from django.conf.urls import url 2>. 基本格式: urlpatterns = [ url(正则表达式, views视图函数,参数,别名), ] url分类: 3>. 无命名分组: url(r'^article/(\d{4 阅读全文
posted @ 2019-05-15 19:31 lancelotxly 阅读(233) 评论(0) 推荐(0) 编辑