• 数据模型续


    # 数据模型
    
    ### 模型关系
    
    - 一对多(使用最多)
      - 一:学生(Student)
        - 添加反向引用
      - 多:文章(Article)
        - 添加外键关联
    - 一对一
      - 一:学生(Student)
        - 添加反向引用(在一对多的基础上多添加属性:`uselist=False`)
      - 一:详情(Profile)
        - 添加外键关联
    - 多对多
      - 多:学生(Student)
        - 需要添加反向引用
        - 需要使用`secondary`指定中间关联表
        - 设置反向查询数据的加载时机,需要使用:`db.backref`
      - 多:课程(Course)
      - 中间关联表:此表不需要用户维护
        - 表名
        - 关联外键
    
    ### 模型总结
    
    - 等价查询
    
      ```python
      @app.route('/query/')
      def query():
          # students = Student.query.all()
          # 与上面的方式等价
          students = db.session.query(Student).all()
          return ','.join(s.name for s in students)
      ```
    
    - group_by:分组查询
    
      ```python
      @app.route('/group/')
      def group():
          from sqlalchemy import func
          ret = db.session.query(Student.id, func.count(Student.id)).group_by(Student.id).all()
          print(ret)
          return '分组查询'
      ```
    
      > 分组查询并统计。
    
    - 指定字段查询
    
      ```python
      @app.route('/select/')
      def select():
          # ret = db.session.query(Student.id, Student.name).all()
          ret = Student.query.with_entities(Student.name).all()
          print(ret)
          return '指定字段查询'
      ```
    
    - 分页查询:paginate,项目中讲解。
    
    - SQL日志:就是查看执行过的SQL语句
    
      ```python
      from flask_sqlalchemy import get_debug_queries
    
      # 记录SQL日志,以下3个配置必须满足一个
      # 调试模式
      # app.config['DEBUG'] = True
      # 测试模式
      # app.config['TESTING'] = True
      # 保存记录
      app.config['SQLALCHEMY_RECORD_QUERIES'] = True
    
      queries = get_debug_queries()
      for q in queries:
          print(q)
      ```
    
    ### 数据缓存
    
    - 说明:
    
      因为数据库的速度是一个web应用性能的瓶颈,因此,为了提高访问效率,尽可能的减少数据库的操作。可以将经常访问的数据缓存起来,再次使用时直接从缓存中获取,而不是每次都操作数据库。
    
    - flask-cache:专门负责数据缓存的扩展。
    
    - 安装:`pip install flask-cache`
    
    - 使用:
    
      ```python
      from flask_cache import Cache
    
      # 配置
      # 缓存类型
      app.config['CACHE_TYPE'] = 'redis'
      # redis主机
      app.config['CACHE_REDIS_HOST'] = '127.0.0.1'
      # redis端口
      app.config['CACHE_REDIS_PORT'] = 6379
      # redis数据库
      app.config['CACHE_REDIS_DB'] = 1
    
      # 创建对象
      cache = Cache(app, with_jinja2_ext=False)
      ```
    
    - 缓存视图函数
    
      ```python
      # timeout:有效期,默认为300s
      # key_prefix:键前缀
      @cache.cached(timeout=100, key_prefix='index')
      def index():
          print('查询数据库')
          return '数据缓存'
      ```
    
    - 清除缓存
    
      ```python
      @app.route('/delete/')
      def delete():
          # 指定删除
          # cache.delete('index')
          # 清空全部
          cache.clear()
          return '缓存已删除'
      ```
    
    - 缓存普通函数
    
      ```python
      # 缓存普通函数时最好指定key_prefix参数
      # 因为不指定时,缓存的键前缀默认是调用的视图函数所在路由
      @cache.cached(timeout=10, key_prefix='aaa')
      def aaa():
          print('查询数据库')
          return 'hello world'
    
      # 缓存普通函数
      @app.route('/common/')
      def common():
          return aaa()
      ```
    
    
    - 自定义缓存
    
      ```python
      @app.route('/test/')
      def test():
          # 先从缓存中获取数据
          data = cache.get('test_data')
          if data:
              # 有缓存,直接返回
              return data
          # 没有缓存
          print('读取数据库')
          data = '123456'
          # 将数据缓存起来
          cache.set('test_data', data, timeout=20)
          return data
      ```
    
      ​
  • 相关阅读:
    idea主题更换pycharm/intellij
    随机生成n张扑克牌。
    JAVA生成6个1-8的随机数,要求无重复。
    一道简单 的循环
    linux虚拟机互访
    linux中grep命令
    vi和vim编辑器
    文件压缩打包以及备份
    文件内容查询
    目录相关操作
  • 原文地址:https://www.cnblogs.com/liangliangzz/p/10222036.html
Copyright © 2020-2023  润新知