Database Transactions with the Django ORM

Josh Dwernychuk
Sightwave Software
Published in
2 min readSep 16, 2017

Django is a great tool for modern web development in python. It’s ORM is simple and reliable. We’ll be looking at a few ways to move data between our application and our database. It’s recommended that we rely on the ORM as much as we can for data processing and extract as much logic from our application code as possible as databases are much better at data processing than python.

Django has a bunch of handy commands that can be referenced in the documentation and we’ll be looking at a few. The API follows the CRUD model and comes with extra methods that allow for more specific implementations.

First off, we’ll define a sample model to be used with our ORM commands:

from django.db import models
class Employee(models.Model):
name = models.CharField(max_length=128)
company = models.CharField(max_length=128)
position = models.CharField(max_length=128)

In order to create a database entry, we’ll be using the create method. The create method takes keyword arguments but does not require them if the model field does not have a NOT NULL constraint.

We can create entries like so:

from .models import Employee# Create Model
employee_1 = Employee.objects.create(name='John', company='Snazzy Tech Startup', position='Software Engineer')
# Save model to db
employee_1.save()
# Create Model
employee_2 = Employee.objects.create(name='Kayla', company='Snazzy Tech Startup', position='Software Engineering Manager')
# Save model to db
employee_2.save()
# Create Model
employee_3 = Employee.objects.create(name='Thomas', company='Snazzy Tech Startup', position='Business Development')
# Save model to db
employee_3.save()

Next we may want to query the database for entries that fit our use case. We can do this with method like filter().

from .models import Employee
employees_that_work_at_snazzy_tech_startups = Employee.objects.filter(company='Snazzy Tech startup')

This will return a django QuerySet object that will contain all entries that fit our desired case.

We can do something similar when we are querying for data and we would only like to retrieve a single response. We do this using the get method.

from .models import Employee
employees_that_are_software_engineers = Employee.objects.get(position='Software Engineer')

This will only return a single entry in the query set.

Contrarily, we can return all items in the database table with the all method like so:

from .models import Employeeall_employees = Employee.objects.all()

We can update our database models with specific methods outlined in the documentation, or with attribute modifications as seen below:

from .models import Employeeemployees_that_are_software_engineers = Employee.objects.get(position='Software Engineer')print(employees_that_are_software_engineers.name)
# John
employees_that_are_software_engineers.name = 'John French'print(employees_that_are_software_engineers.name)
# John French
employees_that_are_software_engineers.save()

Finally, we can delete database entries using the Django ORM with the delete method that can be called on individual entries or QuerySets.

from .models import Employeeemployees_that_are_software_engineers = Employee.objects.get(position='Software Engineer')employees_that_are_software_engineers.delete()

Welp, those are the main methods used when programming with the django ORM. Be sure to check out the documentation for a more in-depth look at all the methods that the ORM provides and clever ways to use them here: https://docs.djangoproject.com/en/1.11/topics/db/queries/

--

--

Josh Dwernychuk
Sightwave Software

Sightwave Software delivers high-quality software solutions for online marketing agencies