How SQL Helped Me Learn Python and the Django ORM

Learning to code at Redbeacon

Jon Hearty
3 min readDec 29, 2013

Getting sick over last Christmas break ended up being a game-changing week, one that would unknowingly set me up for a very successful 2013. With the help of a friend and the determination to remain productive throughout my recovery, I set up Django and started hacking in Python. Looking back, I now realize that the reason I was able to pick things up so quickly was because, faced with a rapidly growing business at Redbeacon and encouraged by then fellow-Wizard Garrett Smallwood, I started learning SQL.

None of this would have been possible without the support of Redbeacon, who gave us access to a read-only version of our database. Because Garrett and I understood Redbeacon’s product inside-out, we were able to quickly learn the data structure (engineers call this “database schema”), which tables contained the data we needed, etc.

Because Django runs a lot of SQL in the background by hiding it under an abstraction known as the Django ORM (object-relational mapping), knowing SQL first allowed me to have a solid starting point - I knew how to do something in SQL and I just had to figure out how to translate it into Python. One of the first concepts I had to understand in SQL was how relational database tables reference each other; in Django, a ForeignKey field is used to link a field from one table to a field (usually the ID field) of another table.

For example, let’s use Django to create a table called “Articles” with 3 fields called “id”, “author_id”, and “time_created”.

class Article(models.Model):
….author = models.ForeignKey(Author)
….time_created = models.DateTimeField(auto_add_now=True)

A few things to note:

  1. We don’t need to specify the “id” field because it is automatically created by Django.
  2. The “author” field represents an object of a class called “Author”, and because it is a ForeignKey field, it is referenced in the database as “author_id.”

Here is what the Articles table might look like:

It is through the ForeignKey relationship that we can write SQL queries that allow us to select the IDs of all articles with a specific author_id; here is what it looks like:

SELECT id FROM articles WHERE author_id = 22

In Django, this would be expressed as a queryset, which would look something like this:

queryset = Article.objects.filter(author_id=22)

This queryset is actually just executing the above SQL statement in the background; whether or not this makes any sense to you is besides the point. What’s important to understand, however, is that because I learned SQL first, I knew not only what was happening (I was selecting articles with an author_id of 22), but also how Django was making it happen using SQL. This understanding would prove critical in shortening my learning curve, increasing my confidence, and helping me understand why some of my code might look pretty on the surface (because the Django ORM abstracts complicated SQL into simple-looking code) despite being inefficient and resourcefully expensive (because I wrote bad code, despite its seeming simplicity).

I’m far from an expert in either SQL (or Python or Django), but learning it gave me the confidence to stop making excuses and start making shit happen. If you desire to learn Django and have the ability to learn and practice SQL first, I would highly recommend it as a first step.

P.S. — If anything here doesn’t make sense, feel free to call me out! As I said, I’m no expert in any of these fields and only hope to convey at a very basic level how I transitioned from SQL to Python/Django.

--

--

Jon Hearty

Spent 8 years selling at early-stage startups @OriginProtocol @Datanyze & Redbeacon. Now focused on building.