PEEWEE 关于视图的操作

以下文字摘录自菜鸟教程:https://www.runoob.com/postgresql/postgresql-view.html

PostgreSQL View(视图)

View(视图)是一张假表,只不过是通过相关的名称存储在数据库中的一个 PostgreSQL 语句。

View(视图)实际上是一个以预定义的 PostgreSQL 查询形式存在的表的组合。

View(视图)可以包含一个表的所有行或从一个或多个表选定行。

View(视图)可以从一个或多个表创建,这取决于要创建视图的 PostgreSQL 查询。

View(视图)是一种虚拟表,允许用户实现以下几点:

  • 用户或用户组认为更自然或直观查找结构数据的方式。
  • 限制数据访问,用户只能看到有限的数据,而不是完整的表。
  • 汇总各种表中的数据,用于生成报告。

PostgreSQL 视图是只读的,因此可能无法在视图上执行 DELETE、INSERT 或 UPDATE 语句。但是可以在视图上创建一个触发器,当尝试 DELETE、INSERT 或 UPDATE 视图时触发,需要做的动作在触发器内容中定义。

CREATE VIEW(创建视图)
在 PostgreSQL 用 CREATE VIEW 语句创建视图,视图创建可以从一张表,多张表或者其他视图。

CREATE VIEW 基础语法如下:

CREATE [TEMP | TEMPORARY] VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE [condition];

您可以在 SELECT 语句中包含多个表,这与在正常的 SQL SELECT 查询中的方式非常相似。如果使用了可选的 TEMP 或 TEMPORARY 关键字,则将在临时数据库中创建视图。

对应的如何的使用PEEWEE操作已创建好的视图呐?

首先是获取查看数据的创建的视图:

print(database.get_views())
 结果:
[ViewMetadata(name='ceshishitu', sql='SELECT youbao_system_student.code,\n    youbao_system_student.school_code,\n    youbao_system_student.class_code\n   FROM youbao_system_student'), ViewMetadata(name='linshji', sql='SELECT youbao_system_student.school_code,\n    youbao_system_student.class_code\n   FROM youbao_system_student')]

比如我们的创建了一个视图之后:

image.png

然后基于上面的这个表(可能其他更复杂的还有多表联合起来的创建的视图)

那此时我们创建的视图的SQL如下:

CREATE VIEW CeshiShitu AS
SELECT code,school_code,class_code
FROM youbao_system_student;
image.png

随后我们的对于的peewee的模型也需要创建:
关于官方文档的说明:


image.png
# 测试视图
class ceshishitu(BaseModel):
    code = TextField(constraints=[SQL("DEFAULT ''::text")], null=True)
    class_code = TextField(constraints=[SQL("DEFAULT ''::text")], null=True)
    school_code = TextField(constraints=[SQL("DEFAULT ''::text")], null=True)

    class Meta:
        #  这个很关键,不然默认的会生产对于的ID 字段表
        primary_key = False

if __name__ == '__main__':
    kkkkk =  ceshishitu().select().dicts()
    print([i for i in kkkkk])
    kkkkk = ceshishitu().select().tuples()
    print([i for i in kkkkk])

对于输入的结果如下:

[{'code': 'XS001', 'class_code': 'B1000000010101', 'school_code': '11111111111111111111111111'}, {'code': 'XS002', 'class_code': 'B10000000201', 'school_code': '11111111111111111111111111'}, {'code': 'XS0021', 'class_code': 'B10000000201', 'school_code': '11111111111111111111111111'}, {'code': 'XS0022', 'class_code': 'B10000000201', 'school_code': '11111111111111111111111111'}, {'code': 'XS0023', 'class_code': 'B10000000201', 'school_code': '11111111111111111111111111'}, {'code': 'XS0024', 'class_code': 'B10000000201', 'school_code': '11111111111111111111111111'}, {'code': 'XS0025', 'class_code': 'B10000000201', 'school_code': '11111111111111111111111111'}, {'code': 'XS0026', 'class_code': 'B10000000201', 'school_code': '11111111111111111111111111'}, {'code': 'XS0027', 'class_code': 'B10000000201', 'school_code': '11111111111111111111111111'}, {'code': 'XS0028', 'class_code': 'B10000000201', 'school_code': '11111111111111111111111111'}, {'code': 'XS0029', 'class_code': 'B10000000201', 'school_code': '11111111111111111111111111'}, {'code': 'XG001', 'class_code': 'B1000000020101', 'school_code': '11111111111111111111111112'}, {'code': 'XG0011', 'class_code': 'B1000000020101', 'school_code': '11111111111111111111111112'}, {'code': 'XG0012', 'class_code': 'B1000000020101', 'school_code': '11111111111111111111111112'}, {'code': 'XG0013', 'class_code': 'B1000000020101', 'school_code': '11111111111111111111111112'}, {'code': 'XG0014', 'class_code': 'B1000000020101', 'school_code': '11111111111111111111111112'}, {'code': 'XG0015', 'class_code': 'B1000000020101', 'school_code': '11111111111111111111111112'}, {'code': 'XG0016', 'class_code': 'B1000000020101', 'school_code': '11111111111111111111111112'}, {'code': 'XG0017', 'class_code': 'B1000000020101', 'school_code': '11111111111111111111111112'}, {'code': 'XG0018', 'class_code': 'B1000000020101', 'school_code': '11111111111111111111111112'}, {'code': 'XG0019', 'class_code': 'B1000000020101', 'school_code': '11111111111111111111111112'}, {'code': 'SADASD', 'class_code': 'ASADSDADASD', 'school_code': '11111111111111111111111112'}]
[('XS001', 'B1000000010101', '11111111111111111111111111'), ('XS002', 'B10000000201', '11111111111111111111111111'), ('XS0021', 'B10000000201', '11111111111111111111111111'), ('XS0022', 'B10000000201', '11111111111111111111111111'), ('XS0023', 'B10000000201', '11111111111111111111111111'), ('XS0024', 'B10000000201', '11111111111111111111111111'), ('XS0025', 'B10000000201', '11111111111111111111111111'), ('XS0026', 'B10000000201', '11111111111111111111111111'), ('XS0027', 'B10000000201', '11111111111111111111111111'), ('XS0028', 'B10000000201', '11111111111111111111111111'), ('XS0029', 'B10000000201', '11111111111111111111111111'), ('XG001', 'B1000000020101', '11111111111111111111111112'), ('XG0011', 'B1000000020101', '11111111111111111111111112'), ('XG0012', 'B1000000020101', '11111111111111111111111112'), ('XG0013', 'B1000000020101', '11111111111111111111111112'), ('XG0014', 'B1000000020101', '11111111111111111111111112'), ('XG0015', 'B1000000020101', '11111111111111111111111112'), ('XG0016', 'B1000000020101', '11111111111111111111111112'), ('XG0017', 'B1000000020101', '11111111111111111111111112'), ('XG0018', 'B1000000020101', '11111111111111111111111112'), ('XG0019', 'B1000000020101', '11111111111111111111111112'), ('SADASD', 'ASADSDADASD', '11111111111111111111111112')]

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容