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.
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):
Open up your shell using the command above.
Now, you need to import the models:
Now, simply run some queries to see what kind of data you retrieve.
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! :)