Django hearts Supabase

Bjoern Holste
3 min readMay 17, 2021

--

Connecting your Django App to Supabase

Photo by Laura Ockel on Unsplash

Are you using Django for your web projects which require server-side heavy lifting? Then you might have come across the fact that Django allows the use of multiple databases.

You then can define for each model to which database it should be saved. While the Django docs are working towards the usecase of having multiple replicas and splitting between user auth and not-so confidential data, another usecase could be to leave the JWT token and API user handling to supabase and don’t bother to stretch out your Django project with a built-in API as well (although this is quite easily done with the REST Framework).

Supabase is Postgres on steroids with lots of awesome helpers already built in. At the time of writing it has just moved to beta. Supabase provides a free try-out version and you can also host it yourself or fire up a droplet on Digital Ocean. If you absolutely had to find one thing not to like about Supabase, it would be that it’s all wrapped up in npm packages which turn your 10 lines of actual code into 1GB on disk… Hey, but no problem if your .gitignore is configured correctly.

I researched this setup for a project where some local data is structured and best saved in a relational database like Postgres running on-premise or internally and other data is unstructured and used by several distributed apps. The initial solution was to stick it in a JSON Field (thanks to José for the awesome JSON widget) in the local DB and build a REST API for others to access it…. but then I had followed Supabase for a while and was happy for a way to play (and too lazy to re-build all the access controls in Django).

So here is how it goes:

In your models.py create the two models and add custom field to indicate in which database they should end up:

class Local(models.Model):
_DATABASE = 'default'
# other model stuff as usual
class Remote(models.Model):
_DATABASE = 'remote'
# other model stuff as usual

Then you need a custom router so create routers.py in the same directory as your settings.py.

Finally, modify your settings.py with the login data provided by Supabase and the path to your router (for me the main directory is config, so the path is config.routers.CustomRouter)

All files in the complete gist at the end of this article :-)

Try it out

To create the tables which hold your models in the respective databases, just run ./manage.py makemigrations and ./manage.py migrate --database=remotedataas usual. This only migrates on the remote db which we use in this example. You’ll have to do the same for the default database to use the Local model here, too.

Start your server with ./manage.py runserverand pull up a shell in a second terminal with ./manage.py shell. In the shell import your model and create a new object like so:

from my_app.models import Local, Remote
l = Local.objects.create(name='Earth', data={'color': 'blue', 'distance': 'near'})
l.save()
r = Remote.objects.create(name='Mars', data={'color': 'red', 'distance': 'very far'})
r.save()

Then head over to your Supabase app, login and check your database. You should see your entry in the table Remote

The details for the remote connection to your Supabase-Postgres can be found under settings->database after you created a database through the GUI.

Have fun coding!

Björn Holste — Technology Institute and Liminalytics

--

--

Bjoern Holste
Bjoern Holste

Written by Bjoern Holste

Entrepreneur, Engineer, Researcher

Responses (1)