Django ORM Queries in the Shell

undraw svg category

In the last year I took over a Django project with about 200k LOC. The team and I have since added almost 100k more LOC in new features. Accordingly, the complexity of our Django models has grown significantly.

Much of the time, when I’m looking to add a new feature, I’ll run through the relationships between models in the shell. It has saved me a ton of headaches and makes working with Django that much more enjoyable.

Here’s how easy it is to jump into the shell and explore your data:

Since I use docker to containerize all my projects, the first couple steps are docker specific.

First, start up your app.

Next, find all the running processes.

The output will look something like this:

Find the CONTAINER_ID that matches the server for your django app. Then execute this command to navigate into the terminal for the correct container:

And finally

Amazing. And now we’re inside the python shell and can start playing with our database. Here’s some example queries that might be useful to start off with.

Or for a different different app…

Want to get down to some raw SQL? No problem

WARNING: No checking is done on the SQL statement that is passed in to .raw(). Django expects that the statement will return a set of rows from the database, but does nothing to enforce that. If the query does not return rows, a (possibly cryptic) error will result.

Resources

Tags:

Previous Post undraw svg category

Advanced NLP with spaCy

Next Post undraw svg category

Doing Data Science in the Browser