Working with Databases in Django

A primer for using Django and the ORM for all your CRUD needs

April 13, 2014, 9:32 p.m.

Django is a web development framework for the python programming language. It is a great way to get a robust web application up and running as quickly as possible. This post will introduce you to Django and show you how you can use it to interact with your database of choice quickly and easily.
Django Basics The Django ORM, or Object/Relational Mapping, is a great tool for database interaction. It allows you to work with your database entries as objects. This is great for easy CRUD (Create, Read, Update, Delete) operations. There are three basic parts of a Django application: Models, Templates, and Views.

Models

Models are the basis of the database. When making your app you will create a model for each table in your database. That model will then become an object for you to work with in your views and templates throughout your app. The following is an example of a model:

Models

There are all sorts of intricacies about models, but for the scope of this post I will keep it simple and just say that models consist of fields that become the columns in your database table.

Views

Views are what you use to prepare content for your site. In a view you can use objects from your database to do various things and finally send them off to your templates. You can also do a whole lot more than that, but again for the scope of this post lets keep it simple. Following is an example of a basic view:

Views

There are many generic views for the CRUD operations that can be easily sub-classed to fit each use cases specific needs.

Templates

Templates are what the end user of your application sees. They take the data given to them from the view and use HTML along with Django's own templating language to display it. Templates allow the user to interact with the database through forms and other means. They are able to send information for CRUD operations back to the views. Following is a basic example of a template.

Template

This template creates the page you are currently viewing! As you can see, templates contain normal program flow like if statements and for loops to display the data to the user. They can also have easy to use forms so that the user can send data back to the view.

Querysets

Although they are not part of the big three, querysets serve an incredibly important role in working with databases in Django. Earlier I said that you would be interacting with objects from the database in your view. Well, the way you receive these objects is through querysets. They are a lot like select statements in SQL, but are much easier to work with thanks to the ORM. Following are some examples:

Querysets

As you can see, some querysets are quite simple and easy to read, while others are more complicated and require some database knowledge to follow. With a complicated app you are bound to have a few difficult querysets, but as you get used to the syntax they start to make more sense.

Well, that's it, you now know the basics of working with databases in Django! If you have any questions or comments go ahead and leave them below, I would love to hear what you thought!

Comments about Working with Databases in Django

No one has left a comment yet, be the first to voice your opinion on Working with Databases in Django!