SQLAlchemy — Hybrid Attributes

James Lewis
4 min readJun 2, 2021

Hey there! Welcome to my blog where I post about my journey as a self-taught developer. You can find my GitHub by clicking HERE.

During a project involving SQLAlchemy and several models in a models.py file, I encountered an issue with one of my columns in the database.

In the web app we have our User Table and users can have multiple vehicles. So we also have a Vehicle Table.

User Table:

Screen shot taken from Author — James Lewis

Vehicle Table:

Screen shot taken from Author — James Lewis

What I was trying to accomplish:

I wanted the user to have a visually appealing experience while they were logged into their account.

In order to add some flare, I thought it would be nice to show the number of vehicles adjacent to their vehicles navigation tab.

Similar to this screenshot below:

Screen shot taken from Author — James Lewis

If you notice the Vehicle tab has a (1) adjacent to it. Allowing the logged in user to know how many vehicles are listed under their account.

Let’s see how we did that in our models.py file!

We first need to import the following at the top of our models.py file.

Screen shot taken from Author — James Lewis

Then we add the @hybrid_property decorator and create a function directly below, which takes a parameter of ‘self’ allowing us to reference a specific column in our table. In our case, this @hybrid_property decorator is nested in our User Table! Therefore, we are able to take any of the User Table Columns and manipulate them.

Screen shot taken from Author — James Lewis

But how do we access the manipulated value? Great question! We access it as we would any other db query. An example below:

Screen shot taken from Author — James Lewis

Do you see what we did? We looped through all of our users, and printed the vehicleLength. This is a small example, but the hybrid attributes in SQLAlchemy are very powerful.

What if you wanted to add an additional parameter? Would you still use the @hybrid_property decorator? A great question! And the answer is no.

There is another attribute more suited for this use case. You would want to use the @hybrid_method decorator.

In the Vehicle Table above, you will notice a column called ‘cmv’. This in my situation represents the Current Market Value of a vehicle. Let’s say for all of the brand new 2021 vehicles, we would want to find how much they depreciated off of the lot and grab their Current Market Value based off of that statistic.

From carsdirect.com!

How Much Can I Expect My New Car to Depreciate? A new car depreciates or loses value almost immediately after you drive it off a dealer’s lot. As a quick rule of thumb, a car will lose between 15% and 20% of its value each year according to Bankrate.com.

Okay, so let’s say some insurance companies will consider the 15% loss and others will consider the 20% loss. For this we use a parameter in our function, and therefore we need to use the @hybrid_method decorator. Let’s check it out below.

Screen shot taken from Author — James Lewis

We take the Current Market Value, and subtract the Current Market Value multiplied by the percentage parameter.

We would then be able to call this specific function with the @hybrid_method decorator like so.

Screen shot taken from Author — James Lewis

There is a lot more to the hybrid attributes in the SQLAlchemy docs located HERE. I recommend reading through just to help spark any ideas. But these two attributes have been very useful for me, and I hope they are for you too.

Happy Coding!

--

--

James Lewis

I am an obsessive autodidact, I love coffee, early mornings, guitar, camping, traveling with my wife and of course…software development!