DEV Community

Cover image for Using HarperDB With Django
Dennis Ivy
Dennis Ivy

Posted on

Using HarperDB With Django

Before you try to use HarperDB in your Django project there are some trade offs you should consider. I wanna cover some of these first and then if you still decide HarperDB and Django are a fit, we'll get into the "how to".

If you're reading this there's a good chance you're coming to this article as a participant of my August Code Battle Hackathon so I will write with those participants in mind.

Tradeoffs

I'll start by saying that at this point in time HarperDB and Django are not exactly suited for each other. Many of the issues are similar to what I said in my video & article about why MongoDB and Django are not a fit, but luckily for HarperDB its in a much better position.

With MongoDB the biggest issue is that there is no database driver for Django and there hasn't been a succesful implementation of converting Django queries into NoSQL queries. As a result of this, you lose A LOT of the features Django has built.

HarperDB has a simular issue. There is no database driver at the moment so we run into the same issue we had with MongoDB. In HarperDB's case it's a SQL & NoSQL distributed database so building this driver is not an impossible task. Lets hope they do it soon ;)

Meanwhile, consider the following:

  • No more Django ORM — With no database adaptor we'll have to use the HarperDB python package instead of the Django ORM.
  • No More Admin Panel — Without an adaptor we cant run the proper migrations we need.

  • Database Connection — The DATABASES connection object in settings.py does nothing for us without a database driver.

  • No more migrations — Cant run migrations without a database driver.
  • Authentication & Models — This part I am still unsure about. If we are no longer using the Django ORM, how will authentication work with the user model? This part may have to be re-written from scratch.

My Suggestion

Use FastAPI, Flask or Custom Functions.

See "FastAPI + React JS App With HarperDB Tutorial"

If you choose to use a lightweight framework you'll have the flexibility you need to make whatever database connection you want. You could also bypass building a backend all together and try using HarperDB custom functions to build an API and connect to the database directly from your frontend.

How To Guide

Source code

I'll assume you already have a HarperDB instance setup with a ready database and a blank Django App ready to go. If not, you can follow me in setting this up in the video tutorial for this article

1 - Install HarperDB Package

You can install the HarperDB python package here. This package will give us the tools we'll need to connect to our database and make queries.

pip install harperdb
Enter fullscreen mode Exit fullscreen mode

2 - Connect to Database

Let's make this connection in settings.py just above the default DATABASES object.

#settings.py
import harperdb

...

DB = harperdb.HarperDB(
    url=<YOUR_HARPERDB_URL>,
    username=<YOUR_HARPERDB_USERNAME>,
    password=<YOUR_HARPERDB_PASSWORD>
)
Enter fullscreen mode Exit fullscreen mode

3 - Query Database

We'll start accessing data in our views by first importing the DB variable from settings.py and then accessing data directly.

#views.py
from django.conf import settings

db = settings.DB

def index(request):
    devs = db.search_by_value('hackathon', 'developers', "id", "*", get_attributes=['*'])
    context = {"devs":devs}
    return render(request, 'base/index.html', context)

Enter fullscreen mode Exit fullscreen mode

Now to render this data in our template you would treat it like any other queryset passed through our context dictionary.

<ul>
{% for dev in devs %}
      <li>{{dev.name}}</li>
{% endfor %}
</ul>
Enter fullscreen mode Exit fullscreen mode

4 - Adding CRUD

Return Single Item

def dev_profile(request, pk):
    dev = db.search_by_hash('hackathon', 'developers', [pk], get_attributes=['*'])[0]
    context = {"dev":dev}
    return render(request, 'base/profile.html', context)
Enter fullscreen mode Exit fullscreen mode

Creating Items

from django.shortcuts import render, redirect

#....

def add_profile(request):
    if request.method == 'POST':
        data = request.POST
        db.insert('hackathon', 'developers', [{"name":data['name']}])
        return redirect('index')
    return render(request, 'base/form.html')
Enter fullscreen mode Exit fullscreen mode

Updating Items

def update_profile(request, pk):
    if request.method == 'POST':
        data = request.POST
        db.update('hackathon', 'developers', [{"id":pk, "name":data["name"]}])
        return redirect('index')

    dev = db.search_by_hash('hackathon', 'developers', [pk], get_attributes=['*'])[0]
    return render(request, 'base/form.html', {"dev":dev})
Enter fullscreen mode Exit fullscreen mode

Deleting Items

def delete_profile(request, pk):
    if request.method == 'POST':
        db.delete('hackathon', 'developers', [pk])
        return redirect('index')

    dev = db.search_by_hash('hackathon', 'developers', [pk], get_attributes=['*'])[0]
    return render(request, 'base/delete.html', {'dev':dev})
Enter fullscreen mode Exit fullscreen mode

And thats it for the demo!

Top comments (6)

Collapse
 
grahamthedev profile image
GrahamTheDev

Wait, you are writing on dev.to, never knew!

Nice article bud! ❤️

Collapse
 
dennisivy11 profile image
Dennis Ivy

Haha yea! Testing between medium and here. I like the markdown aspect here better tho.

Collapse
 
okenk profile image
Oken Keithellakpam

👀 sneak help?
Btw it's great that you're writing here now. I like this one better, also awesome article

Collapse
 
ricaportela profile image
Ricardo Portela

very good article! great! Congratulations!

Collapse
 
sameerahmed123 profile image
sameer-ahmed123

is there going to be a video tutorial as well on youtube?

Collapse
 
dennisivy11 profile image
Dennis Ivy

Just posted it on my channel.