Take note of import we make inside of the application context called routes. That's what I did, and even as I worked through this tutorial, I still came across obnoxious quirks that caught me off-guard. The Flask Application Factory is perhaps one of the most odd and poorly explained concepts in Python software development- my best advice is to not become frustrated, take the copy + paste code above, and blindly accept the spoon-fed nonsense enough times until it becomes second nature. If that last paragraph sounded like total gibberish to you, you are not alone. Within the actual 'application context' is where we'll call create_all(), which we'll cover in a bit. We accomplish this by calling db.init_app() within create_app(), and passing app as the parameter. Even though we've set our db object globally, this means nothing until we initialize it after creating our application. Inside of create_app(), on the other hand, contains the line db.init_app(app). Note the presence of db and its location: this our database object being set as a global variable outside of create_app(). The most basic _init_.py file for Flask applications using Flask-SQLAlchemy should look like this: from flask import FlaskĪpp = Flask(_name_, instance_relative_config=False)ĭb.create_all() # Create sql tables for our data models If you're unfamiliar with the term, you're going to find this tutorial to be confusing and pretty much useless. Initiating Flask-SQLAlchemyĪs always, we're going to use the Flask Application Factory method for initiating our app. We will never have to create engines, sessions, or connections. If you're into some next-level database shit, there are a few other pro-mode configuration variables which you can find here.īy using the exact naming conventions for the variables above, simply having them in our config file will automatically configure our database connections for us. Those are the big ones we should worry about.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |