Test your Queries before they make it into Production using the Django Shell

Posted by Chris Bartos on March 10, 2016

You have a database, you have a Django project and a couple third party Django apps that you installed. You create your models, and now you want to try to test a few queries to see if your models are actually going to work.

Where do I start? How can I test to see if my models are going to work? I wish I could access my database to run some SQL queries with Django to see them in action!

Don’t ever write queries in your Django apps before you are sure they are going to do what you want them to do.

What I tend to do, is I open up my Python interpreter. Oh, not just any Python interpreter, but the one that comes with Django.

$ ./manage.py shell

At this point, you can run some Django queries to see what kind of data you will get. This will allow you see what happens when you perform some queries with a lot faster turn over rate. No need to worry about having to create a fake view, fake template, fake url, etc in order to see what query you need to use.

Let’s see an example.

Suppose we have a model called ‘Animal’ and 2 models ‘Cat’ and ‘Dog’ for some Polymorphism Django ORM magic (maybe you have a animal rescue website, so you need to query both Cats and Dogs in the same ‘Animal’ query):

class Animal(models.Model):
    name = models.CharField(max_length=40)
    created_data = models.DateTimeField(auto_now_add=True)

class Dog(Animal):
    house_broken = models.BooleanField(default=False)

class Cat(Animal):
    litter_trained = models.BooleanField(default=False)

Open up your shell using the command above.

Now, you need to import the models:

import pets.models import Animal, Dog, Cat

Now, simply run some queries to see what kind of data you retrieve.

dog1 = Dog.objects.create(name="Fred", house_broken=True)
dog2 = Dog.objects.create(name="Bob")
cat1 = Cat.objects.create(name="Freckles", litter_trained=True)
cat2 = Cat.objects.create(name="Socks")


animals1 = Animal.objects.all()
animals2 = Animal.objects.filter(created_data__year=2016)

You can do anything you want in the shell. It may take a little longer because you have to write everything out. The great part of this is, you don’t have to create anything other than the models. I created the models in seconds and I wanted to see what these queries would actually do.

Based on what I learned, I’m ready to start a website and start my pet rescue business! :)

What do you do after the Official Django Tutorial?

Here's what you'll learn:

  1. Start writing tests
  2. Increase the size / complexity of the Polls application.
  3. Learn how to code in Django by reading the Django source code.
  4. Finding code snippets to use in your website by reading the code of other websites.
  5. Using Javascript in a web app.
  6. Deploying on Heroku and AWS.
We won't send you spam. Unsubscribe at any time. Powered by ConvertKit

Get some value from this post? Please like and share this post because more people also deserve some value. :-)