Django Works with Doltgres

DOLTGRESINTEGRATION
3 min read

We've spent the past two years integrating Dolt with all your favorite tools in all your favorite languages. We've built many sample applications coupled with Blog posts explaining how to build version control into your application.

These sample applications have proven useful in testing Doltgres as well. Today, we're happy to announce that Django, a popular Python web framework, works with Doltgres. Getting the sample application working with Doltgres instead of Dolt simply requires a configuration change which is a testament to Doltgres' compatibility with Postgres but also Django's ORM capabilities translating MySQL SQL into Postgres SQL.

Django + Doltgres

Just Run the Sample Application!

It's best to poke around with the sample application to see Doltgres' version control capabilities in action with Django. Follow these steps to get it working.

  1. Install dependencies
  2. Clone the sample repository, doltgres branch, from GitHub
  3. Start a doltgres server and create a database called mydatabase
  4. Run migrations using python manage.py migrate to create the required database tables
  5. Create an admin user using python manage.py createsuperuser
  6. Start the webserver using python manage.py runserver
  7. Hit http://localhost:8000/polls or http://localhost:8000/admin

The application is a simple Polling website

Django Sample Application

The admin console is where the version control functionality is used. You can create branches, make changes, and then merge your changes back into the main branch.

Django Admin Interface

Install Dolt, Python, Django, and the Python Postgres client.

To get this demo going, we're going to need Doltgres, Python, Django, and the Python Postgres client (ie. psycopg2).

Let's start with Doltgres. Download doltgres from the GitHub releases page and make sure it is on your PATH.

$ which doltgres 
/Users/timsehn/go/bin/doltgres

For the rest of this blog entry, I'm going to assume you are on a *NIX based system and use bash when interacting on the command line.

I already had Python on my Mac, installed via Homebrew. I also take care to make sure it is the default Python used when I type python in a terminal.

$ which python
python: aliased to /opt/homebrew/bin/python3

From there, I need Django. The Django install docs recommend running:

$ python -m pip install Django

which did the trick for me. The last thing I needed was psycopg2 which can be installed with:

$ python -m pip install --break-system-packages psycopg2-binary

I tried a bunch of different options and this was the only way to get my install to work. I needed the binary installation and had to use the --break-system-packages option. All the other ways I tried to get psycopg2 installed failed building against my Homebrew installed Postgres. You may be more successful with your Python set up.

With that you should be good to go on dependencies for the rest of this article.

Django Sample App

Now, it's time to go back to the original article and see how we built the Django sample application against Dolt. All the same principles apply to Doltgres. In fact, the code works with the following simple configuration changes:

--- a/dolt_django/settings.py
+++ b/dolt_django/settings.py
@@ -77,12 +77,12 @@ WSGI_APPLICATION = 'dolt_django.wsgi.application'
 # https://docs.djangoproject.com/en/5.0/ref/settings/#databases
 DATABASES = {
     'default': {
-        "ENGINE": "django.db.backends.mysql",
+        "ENGINE": "django.db.backends.postgresql",
         "NAME": "mydatabase",
-        "USER": "root",
-        "PASSWORD": "",
+        "USER": "postgres",
+        "PASSWORD": "password",
         "HOST": "127.0.0.1",
-        "PORT": "3306",
+        "PORT": "5432",
     }
 }

One recommended change is to use Doltgres functions instead of procedures when calling Dolt version control functionality. Custom Dolt procedures still work in Doltgres but in Postgres, procedures cannot return results. For that reason, Dolt version control functionality has been migrated to functions. So, in Doltgres you select dolt_checkout('main') to change the branch for your session to the main branch instead of call dolt_checkout('main') like you would in Dolt. Having a select query modify database state is still weird to us but apparently, this is idiomatic Postgres.

Conclusion

Django works with Doltgres. You can build a version controlled Django web application using Dolt or Doltgres. Want another framework to work with Doltgres? Come by our Discord and just ask.

SHARE

JOIN THE DATA EVOLUTION

Get started with Dolt

Or join our mailing list to get product updates.