Defining objects in Flask Python

Having a database to be connected to is a great first step. Now it’s time to define some objects to populate the database. In application development, a “model” refers to a real or conceptual representation of some object. For example, if you’re making an app for a car dealership, you might define a model business owners cell phone numbers car that encapsulates all the attributes and behaviors of a car. In this case, you will be making a To-do List with Tasks, and each Task belongs to a user. Before you think further about how they are related, start by defining the objects for Tasks and Users.

Model relationship

In certain web applications, you might want to express relationships between objects. In the To-do list example, users own several tasks, and each task belongs to only one user. This is an example of a “many-to-one” relationship, also known as a foreign key relationship. Where the tasks are the “many” (many) and the user who owns them is the “one” (one). In Flask Python, a many-to-one relationship can be specified using the db. Relationship function . First, build the User object. It looks quite similar to the Task object; you will find that many objects have the same basic class attribute formats as column tables. Every now and then you’ll come across something a little different, including multiple inheritance magic, but that’s the norm.

business owners cell phone numbers

Initializing the database

Now that the models and model relationships are set up, start configuring your DV Leads. Flask Python doesn’t come with its own database management utility, so you’ll have to write your own (up to a point). You don’t need to spruce up much; you just need something to recognize which tables will be created and some code to create them (or drop them if need be). In case you need something more complex like handling database table updates (eg database migrations) you might want to take a look at tools like Flask Migrate or Flask-Alembic . Create a script called initializedb.py next to setup.py to manage the database. (Of course, it doesn’t have to be called that, but why not give it names that are appropriate for the file’s function?) Inside initializedb.

Leave a comment

Your email address will not be published.