![]() This article will show you how to integrate SQLAlchemy 1.4 with Flask 2.1. Besides, you will gain better type hints for SQLAlchemy model, and possibly easier migration to SQLAlchemy 2.x. However, if you are developing a rather simple project with Flask and SQLAlchemy, and do not want to depend on another third-party library, or you prefer using SQLAlchemy directly, making the model layer agnostic of web frameworks, you can write your own extension. Return render_template('index.When it comes to connecting to database in Flask project, we tend to use the Flask-SQLAlchemy extension that handles the lifecycle of database connection, add a certain of utilities for defining models and executing queries, and integrate well with the Flask framework. # -*- coding: utf-8 -*-įrom flask import render_template, redirect The following code should produce the same effect without the use of events or the g proxy. This pattern is commonly used with an ORM to hide the underlying management of the database connection and reduce boilerplate code. The pattern, as shown below, is used to accomplish exactly the same thing as would be achieved by using a context manager (or try, except, finally block) to close the database connection within your function. This prevents the database connection being left open if an error occurs in your application code. ![]() The g proxy is merely used to store your database connection, which was created in your view function, and makes it available to the close_connection function where it is guaranteed to be closed. There are many other events that can be hooked to execute before or after your view function to provide pre- and post-processing of the request or response. In your code you use a decorator to register the function close_connection with the teardown_appcontext event, thus guaranteeing it is called after completion of handling of every web request. This is used to pass information between functions called by flask as a result of events and signals. The g proxy is stored with the application context and it's used to store variables with a lifetime of that particular web request. The request context contains data specific to that particular web request such as form data, remote ip address, http headers, etc.Įvery time a web request is received, new application and request contexts are created before your view function is called and these are then destroyed after a response has been returned and the processing of the web request has been completed. So, could anyone provide a correct case of using plain sql with separate connection function?įlask application and request contexts behave like global variables for the purposes of your code, but are in fact proxies that will contain different values every time your application receives a new web request. So as I understand "get_db" is accessible from our request context and we're able to connect to the db and use that g value in all functions inside the current tread? What about "close_connection"? Because of decorator "teardown_appcontext" the connections have to been closed after app context is failed or ended? Will it work in my code? In routes.py we get query from client with root route("/") so in that moment request context is created and after that application context as well. So in init.py we create "global variable" g which is in application context and should be accessible for each tread in request context. ![]() The main problem that I don't understand what is a "request" in that case. I'm just trying to catch how the application and the request contexts work in that case with using g. I've copied "get_db" and "close_connection" functions from the official site of Flask framework, but I'm not clearly understand how it works and what will be the difference if I use just simple code for "def get_db" without "g" like this: con = nnect("catalog.db")Īnd obviously close connections after query is done like this: cur.close() Return render_template('index.html',title='DBSelectTest', posts=select) Now I want to make out how to manage DB connections without Sql-Alchemy and other ORMs, just with plain sql.ĭb = g._database = nnect('test.db')įrom flask import render_template,redirect I'm newbie in Flask and web programming on the whole, so I'm trying to understand the principles of contexts and db operations of the framework.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |