Data Model Model relationships
- One-to-many (most used)
- One: Student (Student)
- You need to add a reverse reference
- MORE: Article (article)
- You need to add a foreign key association
- One
- One: Student (Student), Main table
- You need to add a reverse reference to specify more than one property in a one-to-many scenario
userlist=False
- One: Details (profile), secondary table
- You need to add a foreign key association
- Many-to-many
- Many: Students (Student)
- You need to add a reverse reference
- Adding a reverse reference is required by
secondary
specifying an intermediate association table
- Set the query time for the reverse reference, which can be
db.backref
done by completing
- Multi: Course (Course)
- Intermediate association table: Students choose the timetable, do not need to operate and maintenance
- Fields: Table name, foreign Key Association
Model Summary
Equivalence query
@app. Route ('/query/') def query(): # students = Student.query.all () # equivalent toStudents = Db.session.query (Student). All ()return ', '. Join (S.name forSinchStudents
Specify field Query
@app. Route ('/select/') def select(): # ret = Db.session.query (student.id, Student.name). All () # Specify field query, equivalent to the aboveret = Student.query.with_entities (student.id, Student.name). All ()# Returns a list of tuples consisting of a specified fieldPrint (ret)return ' End of query '
Paged query: Paginate, the project explained.
View SQL log: is to view executed SQL statements.
# Log SQL logs, you need to meet any one of the following three conditions# app.config[' DEBUG ' = True# app.config[' testing ') = Trueapp.config[' Sqlalchemy_record_queries '] =True fromFlask_sqlalchemyImportGet_debug_queriesqueries = Get_debug_queries () forQinchQueries:print (q)
Data caching
Description
The speed of a database is a performance bottleneck for a Web application, so in order to improve access efficiency, you should minimize access to the database. You can cache frequently accessed data, fetch data from the cache each time it is accessed, and return it directly, and no longer read from the database.
Flask-cache: An extension specifically responsible for data caching.
Installation:pip install flask-cache
Use:
fromFlask_cacheImportCache# Configuration# Cache Typeapp.config[' Cache_type '] =' Redis '# Hostapp.config[' Cache_redis_host '] =' 127.0.0.1 '# portapp.config[' Cache_redis_port '] =6379# Databaseapp.config[' cache_redis_db '] =1# Create ObjectsCache = cache (app, with_jinja2_ext=False)
Cached View functions:
@app. Route ('/')# timeout: Specify cache expiration, default 300s# Key_prefix: Cache key prefix, default: view/+ routing Address@cache. Cached (timeout=100, key_prefix= ' index ') def index():Print' Read database ')return ' valid data '
To cache normal functions:
# to cache normal functions, Key_prefix must specify@cache. Cached (timeout=100, key_prefix= ' common ') def Common():Print' Query database ')return ' returned data '@app. Route ('/hello/') def Hello(): returnCommon ()
Clear Cache
@app. Route ('/clear/') def Clear(): # Specify Delete # cache.delete (' index ') # empty AllCache.clear ()return ' cache is cleared '
Custom Cache
@app. Route ('/zidingyi/') def Zidingyi(): # Get it from the cache firstdata = Cache.get (' Zidingyi_data ')ifDatareturnData# No cached dataPrint' Get data from the database ') data =' 123456 ' # Cache DataCache.set (' Zidingyi_data ', data, timeout= -)returnData
?
?
flask:06-time to master a song Flask data model (02)