Django features you did not know existed!

ยท

6 min read

Django features you did not know existed!

Photo by Faisal on Unsplash

So software engineers have to work with various technologies & frameworks while creating new pieces of software. Often times we work on some frameworks or libraries that have some handy feature but are not aware of it.

So is the case with the Django framework. It's been a few years working with the Django framework. Recently I have started looking into Django in-depth & found some features I did not know or it was there in the documentation but I never paid attention to them. So in this article, we will discuss some amazing features provided by the Django framework which I think many have missed & or are not aware of it.

Most of the items on the list are commands only which are provided by Django & you can run them using the terminal. So let's get started...

dbshell command.

This is my personal favorite feature/command provided by Django. I can't put more emphasis on how handy & important this command is.

dbshell command gives you a terminal interface to directly interact with your database, it is awesome, isn't it?

It will read the database configuration from your settings file & will open a terminal. You can execute a valid SQL query there. it comes very handy when I need to test some SQL queries quickly. Don't need to open any GUI tools to test queries anymore. You can use this command like this,

python manage.py dhshell

Screenshot_20220826_210125.png

shell command.

Another favorite command of mine. It is similar to dbshell but instead of giving to terminal interface to your database, it opens up a python interpreter. You might be wondering what's new in this I can open a python interpreter using the python command line myself. Yes, you are right but it is not a simple Python interpreter, it actually gives you access to your Django project, basically, it is Django app in the command line.

You can actually interact with your Django app directly using this. You can run any code, you can import your models & directly run them as you do it in your code.

It is just a very nice way to test some code quickly which is tied to the Django framework. I often use this to run model factory code to quickly populate tables with dummy data.

inspectdb command.

This one is a very interesting feature & discovered it very late in my career. This command actually generates model classes for you. If you already created all the database tables & now want to create model files for each table then you don't need to do it manually. inspectdb command will look into your database find all the tables & will generate model classes for you on the terminal console. You can save it to a file very easily. Let's look at the sample model code generated by it, I have an SQLite database & table named persons.

Now if I run this command,

python manage.py inspectdb

I get this output for persons table,

class Persons(models.Model):
    name = models.CharField(max_length=255)

    class Meta:
        managed = False
        db_table = 'persons'

As you can see it has perfectly generated model code for my table. Django tries its best to convert a given type in the database table to Python's type field but it is not perfect. It is advisable to look at the model code after generation. One more thing to note is that Django mark that model as managed = False which means it will not manage any migration for this model but you can make it True if you want. Read more about it here

check command.

check is a very useful command which checks your Django app for common mistakes which can be over sighted by you. It checks for things like proper database configuration, cache configuration, check model fields, etc.

When running a runserver command it runs the check command before that performing a system check before running the server. This is the reason you get errors on the console while running the local server & notifies you about the issues in the app. The interesting thing about this is that it is extensible. You can write your custom checks if you need any. Read more about it here

ping_google command.

If you have built any blog or content management system where you publish public content then you must be aware of the sitemaps.xml file.

sitemaps.xml file is nothing but a public record that tells a search engine(Google) how many pages you have on your website & how to index them. This is very important from an SEO point of view but that is the topic for later.

Django provides sitemap.xml support using the sitemaps framework in built-in Django. Once you have added the sitemap.xml to your website you have to wait for Google to read the file and re-index your site if new content is added. Or you have to manually do it from Google search console & we don't want to do manual work, right?

So here comes this handy feature in Django ping_google using you can ping google to let it know that your sitemap.xml file is updated & you want to re-index it.

Read more about it here

sendtestemail command.

It is another useful command to quickly verify if you can send emails through your Django app. It reads the email settings from your settings.py file. Often times we set up SMTP configuration & it does not work right away. To test it we have to go through the app feature or API (personal experience ๐Ÿ˜…).

Say no more to this type of testing & just run this command & test your SMTP configuration.

python manage.py sendtestemail john@example.com doe@example.com

Read more about it here.

clearsessions command.

This command is very straightforward as the name suggests it clears the expired sessions. This is particularly useful if you are using the database to store session data. The session data can grow exponentially & you don't want to retain it forever so can run the command as a cronjob to clear the expired sessions.

changepassword command.

This is another neat command to quickly change the password for a user from the command line directly. This will only work if you use Django's own authentication system which is django.contrib.auth.

You can use the command like this,

python manage.py changepassword [<username>]

GeoDjango

This is not a command provided by Django. This is actually a contrib module or you can say a mini framework within Django to deal with Geo Spatial data.

Nowadays there is a rise in startups or services that deals with Geo Spatial data. The best examples are food delivery startups. They have to store and process geographic data & have to deal with vector geometry calculations.

This module provides some neat features to make the developer's life easier to deal with this type of data. It provides specialized model & model fields to create store geospatial data & ORM extension to do query & data manipulation of Geo-Spatial data.

I haven't used it personally yet but it looks promising. I am assured that in case I have to work on a project which deals with geospatial data then I don't need to look outside Django.

Hope you learned something new today after reading this...

ย