A Guide to Writing Class Models in Flask for Beginners

Hello, and welcome to my blog about class models in Flask. In this blog, I will be discussing the basics of how to write class models in a way that is easy to understand for beginner and experienced coders alike.

Where to Start?

When writing out your class models in Flask the best place to start is with the data models that show a visual representation of what the class relationships will look like. Let us view an example of that first so we can see what code needs to be written.

Breaking Down the Models

Now that we know what our data models look like we can start to understand how they are communicating with one another. To start we can see that signups are our intermediary table meaning that signups belong to both campers and activities. We can now look to activities to see that activity has many signups, and has many campers through signups. This means campers also has many signups, and many activities through signups.

Writing the Base Code

The next step would be to write out the base class models so let us do that here.

class Activity(db.Model, SerializerMixin):
    __tablename__ = 'activities'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    difficulty = db.Column(db.Integer)

The Activity class contains:

  • An id that is an integer and primary key

  • A name that is a string representing the activity name

  • A difficulty that is an integer representing the difficulty level of the activity

class Camper(db.Model, SerializerMixin):
    __tablename__ = 'campers'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=False)
    age = db.Column(db.Integer)

The Camper class contains:

  • An id that is an integer and primary key

  • A name that must exist as a string representing the camper's name

  • An age that is an integer representing the age of the camper

class Signup(db.Model, SerializerMixin):
    __tablename__ = 'signups'

    id = db.Column(db.Integer, primary_key=True)
    time = db.Column(db.Integer)
    camper_id = db.Column(db.Integer, db.ForeignKey("campers.id"), nullable=False)
    activity_id = db.Column(db.Integer, db.ForeignKey("activities.id"), nullable=False)

The Signup class contains:

  • An id that is an integer and primary key

  • A time that is an integer representing the hour of the day that the activity will take place

  • A camper id that must exist as an integer and a foreign key representing a campers id

  • An activity id that must exist as an integer and a foreign key representing an activities id

Writing the Relationships

Now that we have the base of our class models written we can start to write the relationships of the tables. Let us first start with the intermediary table signups.

camper = db.relationship("Camper", back_populates="signups")
activity = db.relationship("Activity", back_populates="signups")

Here we are writing that signups belong to a Camper and an Activity using db.relationship().

Next, we will write the relationships of the activities.

signups = db.relationship("Signup", cascade="all,delete", back_populates="activity")
campers = association_proxy("signups", "camper")

In activities, we are linking one activity to signups using db.relationship() and we are linking a camper through signups using association_proxy().

signups = db.relationship("Signup", back_populates="camper")
activities = association_proxy("signups", "activity")

Lastly in campers, we link one camper to signups using db.relationship() and link an activity through signups using association_proxy().

Writing the Serialize Rules

If you have made it this far congratulations you have now created very basic class models and their relationships. There is only one thing left to do that is required which is to write the serialize_rules. Some people find these to be very confusing so make sure to follow along closely to not get lost.

Let us start with the activities table.

serialize_rules = ("-signups.activity",)

This may not seem like a lot but there are a few key things to notice here. First, when there is only one serialize_rules we must remember to add a coma due to tuples needing to have multiple values. Secondly, we are using a - in front of our rules which is mandatory when using serialize_rules. Lastly, we are backtracing our steps to make sure we stop maximum recursion from occurring. So, since we are inside of Activity we are going to go through the signups relationship to Signup and then through activity to Activity and that is where we want to end our journey. The reason we do this is so that we do not create an infinite loop.

Let us now do the campers and signups tables.

serialize_rules = ("-signups.camper",)

In the Camper class, we are doing something very similar to activities in which you can refer to the steps above for guidance.

serialize_rules = ("-camper.signups", "-activity.signups")

However, in the Signup class, we are doing something slightly different. Here you can see we are backtracing our steps but instead starting at the intermediary table and working our way outwards.

In conclusion, understanding class models in Flask is essential for building effective and organized applications. By following the steps outlined in this blog, you can create clear and concise class models that facilitate communication between different data tables.