Old Boys video --flask_SQLAlchemy knowledge

1. There are a py file in front of a = __all__ [ ' '], indicates that the py file can only export this class *, use: If a py file has a lot of things, a lot of class, you just want to let people import a class I used __all__
when 2.flask SQLAlchemy query, which is equivalent to label as, that is for another name.
When querying the database
filter_by pass parameter, filter expression transfer

r3 = session.query(Users).filter(Users.name == “alex”).all()
r4 = session.query(Users).filter_by(name=‘alex’).all()

It refers to a non ~ Users this condition

ret = session.query(Users).filter(Users.id.in_([1,3,4])).all()
ret = session.query(Users).filter(~Users.id.in_([1,3,4])).all()

Mysql wildcard% accounted for many of the characters, underscore _ accounting for a character

ret = session.query(Users).filter(Users.name.like(‘e%’)).all()
ret = session.query(Users).filter(~Users.name.like(‘e%’)).all()

If you do packet filtering for conditions after group_by, having to use

ret = session.query(
func.max(Users.id),
func.sum(Users.id),
func.min(Users.id)).group_by(Users.name).having(func.min(Users.id) >2).all()

The default table is even join inner join, isouter is true for the external connection, join default conditions inside the outside through the associated key.

ret = session.query(Person).join(Favor, isouter=True).all()

Added in one portion to a plurality of objects in the session, using add_all ()

session.add_all ([
Hobby (Caption = 'ping-pong'),
Hobby (Caption = 'badminton'),
the Person (name = 'John Doe', hobby_id =. 3),
the Person (name = 'John Doe', hobby_id = 4 ),
])

git

On-line system developed after the problem can roll back to the previous version

flask_script

Custom commands, write scripts. Such as for Python manager.py the runserver or Python manager.py the runserver -H -p
(. 1) first from flask_script Import Manager
(2) Register Manager Manager = Manager (App)
(. 3)

if __name__ == '__main__':
  			  manager.run()

manager.py as follows:

from flask import Flask
from flask_script import  Manager

app = Flask(__name__)
manager = Manager(app)

@manager.command
def custom(arg):
    """
    自定义命令,自定义脚本,相当于位置参数
    python manager.py custom 123
    :param arg:
    :return:
    """
    print(arg)

@manager.option('-n','--name', dest='name')
@manager.option('-u','--url', dest='url')
def cmd(name,url):
    """
    自定义命令   相当于关键字参数
    python manager.py cmd -n zhangweijian -u http://www.baidu.com
    :param arg:
    :return:
    """
    print(name,url)

@app.route('/')
def hello_world():
    return 'Hello World!'


if __name__ == '__main__':
    manager.run()

flask_migrate database migration

Named database migration

python manager.py db init
python manager.py db migrate
python manager.py db upgrade

(1) first from the Migrate flask_migrate Import, MigrateCommand
(2) register the migrate the migrate = the Migrate (App)
(. 3) manager.add_command ( 'DB', MigrateCommand)
each database has a table is alembic_version, is submitted record table. Recorded in the corresponding folder inside migration

Guess you like

Origin blog.csdn.net/xili2532/article/details/90815137