python - Indenting db.commit() in sqlalchemy script -
I know this should be a primary question, but for some reason I do not understand this I do not understand Is not indented under the statement for line code def insert_roles ():
, which I do not understand because nothing is being done except for specifying dict keys and values I will post the entire code and what the book says below about it thanks in advance for any assistance. . .
class permission: follow = 0x01 COMMENT = 0x02 WRITE_ARTICLES = 0x04 ModerTom composite = 0x08 admin = 0x80 square role (DB model): __tablename__ = 'roles' id = db.Column (db Integer, primary_key = true) name = db Columns (DB String (64), Exclusive = True) Default = DB. Permission = db.Column (db.Integer) user = db.relationship ('user', backf = 'roll', lazy = 'dynamic')) column (db. Boolean, default = false, index = true)) @staticmethod def Insert_roles (): roles = {'user': (permission; permission; permission; permission; permission; WRITE_ARTICLES, true), 'moderator': (permission; follo | permission com. | Permission. REITE_ARTICLES | permission. MODERATE_COMMENTS, incorrect ), 'Administrator': For r in roles (0xff, wrong): role = Role.query.filter_by role (role = role (name = r) role. permissions = roles [ R] [0] role.default = roles [r] [1] db.session.add (role) Db.session.commit () def __repr __ (self): Return '& rt; role% r & gt;' % Self.name
From the book:
insert_roles ()
function does not directly create new role objects, instead it A new role object is designed only for role names that are not already present in the database. This is done so that the future changes list Can be updated when changes need to be made. Add a new role or change the permission assignment for a role, change the roles and run the function again. Note that the "Anonymous" role is not required to be presented in the database, as it is designed to represent users who are not in the database.
db.session.commit ()
is outdent , After the loop has expired, it is necessary to run After - adding each role to the DB session is the command that essentially writes information in the database "permanently "- add ()
function stores data in temporary memory, but commit ()
actually reduce it The disk is Writing in memory is a very cheap campaign, is more expensive for discs, so you want to save that step by the end. Does that mean?
Comments
Post a Comment