Django generic foreign keys for beginners

Duration: 7 minutes
Level: beginner
Objective: understand the principle behind a generic foreign key and how to use it in Django

The Problem

Let’s say we are having a table in our database named Device which holds information about our users devices. Some information is common (a Foreign Key to the owner, the version of the os, etc), but other is specific to the device type (IOS and Android).

The question is: how are we going to represent this in our database?

Solution 1: Very bad!

Screen Shot 2017-09-01 at 10.41.47 AM.png

We could hold all the information in the same table. When we are dealing with an IOS device, all the android_specific fields will be empty and vice-versa.  This can work in simple cases (like a school project), it’s true.

Explaining how this approach is breaking the basic principles of relational databases it’s outside the scope of this post – but trust me, it’s bad -.

Moreover, there are many inconveniences with this approach. You have to check every time the type when using an instance of Device. If by accident, a bug in your code sets the ios_specific fields for and Android device, things can get nasty. Adding other types (Windows Phone for example) will add even more columns in the table which you will not use.

Solution 2: Better, but still bad!

Screen Shot 2017-09-01 at 10.56.08 AM.png

We split the specific info into separate tables. In the main device, we hold a one-to-one relation with each of the table. For every instance, we have to make sure that only one of the foreign keys is not None. Better, but we’re not quite there yet.

Solution 3: Best!

Ideally, we want to have a single foreign key through which we can access the platform specific information. Even more, we want to be sure that when we are instantiating or updating an instance of Device, the platform specific table is set behind the scene.

Please welcome to the stage:  Generic Foreign Key

Screen Shot 2017-09-01 at 11.09.00 AM.png

One foreign key – platform_specific_info (I suck at naming, it’s a common problem of software developers 🙂 ) – which reference to either AndroidDevice or IosDevice depending on the device type. No information is duplicated and no columns are left empty in the database.

Let’s see how this is implemented in Django.

Implementation in Django

It’s clear that this is not just a simple foreign key that holds the id of the row in the other table. In addition to that, it has to know from which table to take that id. So a generic foreign key would look like this (table_id, row_id).

A short introduction to ContentType

Luckily for us, Django got us covered with the table_id. It’s a table called content_type which comes Django and holds references to all other tables in the database. Just make sure you have 'django.contrib.contenttypes' in your INSTALLED_APPS from

Let’s see how to code will look like in the

Screen Shot 2017-09-01 at 11.59.07 AM.pngThings are looking good in the db, but let’s make sure that the platform_specific_info will reference the prober table every time. Mixing a different type with a different table can cause serious damage.  Let’s add this method in the same file:

Screen Shot 2017-09-01 at 12.10.52 PM.png

Great! Now we are sure that in any circumstance, the platform_specific_info is pointing to the right table.

Understanding the principle first makes the implementation a real pleasure.

Please leave your questions and feedback bellow or subscribe here for more weekly web wisdom!

See you soon!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s