Skip to Content
Flask Web开发:基于Python的Web应用开发实战(第2版)
book

Flask Web开发:基于Python的Web应用开发实战(第2版)

by Miguel Grinberg
August 2018
Intermediate to advanced
230 pages
6h 42m
Chinese
O'Reilly Media, Inc. Beijing
Content preview from Flask Web开发:基于Python的Web应用开发实战(第2版)
9
用户角色
Web
应用中的用户并非都具有同等地位。在多数应用中,一小部分可信用户具有额外权
限,用于保障应用平稳运行。管理员就是最好的例子,但有时也需要介于管理员和普通用
户之间的角色,例如内容协管员。为此,要为所有用户分配一个
角色
在应用中实现角色有多种方法。具体采用何种实现方法取决于所需角色的数量和细分程
度。例如,简单的应用可能只需要两个角色,一个表示普通用户,一个表示管理员。对于
这种情况,在
User
模型中添加一个
is_administrator
布尔值字段可能就够了。复杂的应
用可能需要在普通用户和管理员之间再细分出多个不同等级的角色。有些应用甚至不能使
用分立的角色,赋予用户一系列独立的
权限
或许更合适。
本章介绍的用户角色实现方式结合了分立的角色和权限,赋予用户分立的角色,但是各个
角色都通过权限列表定义允许用户执行的操作。
9.1
 角色在数据库中的表示
5
章为了演示一对多关系,创建了一个简单的 roles 表。示例
9-1
是改进后的 Role 模型。
示例
9-1
 
app/models.py
:角色数据库模型
class Role(db.Model):
__tablename__ = 'roles'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), unique=True)
default = db.Column(db.Boolean, default=False, index=True) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Python机器学习基础教程

Python机器学习基础教程

Andreas C. Müller, Sarah Guido
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano
程序设计导论:Python语言实践

程序设计导论:Python语言实践

罗伯特 塞奇威克, 凯文 韦恩, 罗伯特 唐德罗

Publisher Resources

ISBN: 9787115489456